gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:35:31 24 4
gpt4 key购买 nike

我收到一个错误。

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com