python - 类型错误 : as_view() - Django

标签 python django web-services django-rest-framework geodjango

我收到一个错误。

我不知道如何修复它,看看,这就是错误:

TypeError: as_view() takes 1 positional argument but 2 were given

正如您所看到的,这是我的“model.py”页面的代码。

from django.db import models
from django.contrib.gis.db import models


class RoadsLines(models.Model):
    gid = models.IntegerField()
    geom = models.MultiLineStringField()

    def __str__(self):              # __unicode__ on Python 2
        return '%s %s' % (self.gid, self.geom)

正如您所看到的,这是我的“views.py”页面的代码。

from django.shortcuts import render
# Create your views here.
from django.shortcuts import render
from rest_framework import generics
from world.models import RoadsLines
from world.serializers import RoadsLinesSerializer


class ListCreateRoadsLines(generics.ListCreateAPIView):
    queryset = RoadsLines.objects.all()
    serializer_class = RoadsLinesSerializer

正如您所看到的,这是我的“urls.py”页面的代码。

from django.conf.urls import url, include
from rest_framework import routers, serializers, viewsets
from world import views

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'Roads', views.ListCreateRoadsLines)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api/', include('rest_framework.urls', namespace='rest_framework'))
]

我做错了什么?

提前谢谢您!

最佳答案

ListCreateRoadsLines 是一个 View ,而不是 View 集。您应该将其包含在您的 url 模式中,而不是尝试注册它:

urlpatterns = [
    url(r'^Roads$', views.ListCreateRoadsLines.as_view()),
    url(r'^', include(router.urls)),
    url(r'^api/', include('rest_framework.urls', 
    namespace='rest_framework'))
]

关于python - 类型错误 : as_view() - Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44552660/

相关文章:

python - Django Rest API POST 问题

django - 由于某种原因,google oauth2 可以使用不同的 gmail 电子邮件注册同一用户

java - 如何为现有站点创建 Restful 服务

python - 通过单击两个键以 OpenCV VideoCapture 结束时发出关闭视频的问题

python - 相当于 `--find-links` 中的 `setup.py`

python - 将焦点放在 QlineEdit 小部件上

python - 我如何清除 celery 中的所有东西?

python - 获取在 django 中使用 "upload_to"的上传媒体文件的路径

java - Web 服务最重要的用途

web-services - 使用axis1 的Web 服务客户端的Maven 依赖项