gpt4 book ai didi

django,名称 'IndexView' 未定义

转载 作者:行者123 更新时间:2023-12-01 09:54:27 27 4
gpt4 key购买 nike

我关注 this tutorial .目前我在this点但是当我用 python manage.py runserver 0.0.0.0:8000 启动我的服务器并在我的浏览器中打开 url 时,我收到以下错误:

name 'IndexView' is not defined

这是我的 urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import patterns

from rest_framework_nested import routers
from authentication.views import AccountViewSet

router = routers.SimpleRouter()
router.register(r'accounts', AccountViewSet)

urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)),

url(r'^api/v1/', include(router.urls)),
url('^.*$', IndexView.as_view(), name='index'),
)

我不知道如何解决这个问题,因为我从未见过自己在某处声明过这个 IndexView。如果你们能给我一些关于这个的建议,那就太棒了。

编辑:

我的意见.py

from django.shortcuts import render

# Create your views here.

from rest_framework import permissions, viewsets

from authentication.models import Account
from authentication.permissions import IsAccountOwner
from authentication.serializers import AccountSerializer

class AccountViewSet(viewsets.ModelViewSet):
lookup_field = 'username'
queryset = Account.objects.all()
serializer_class = AccountSerializer

def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return (permissions.AllowAny(),)

if self.request.method == 'POST':
return (permissions.AllowAny(),)

return (permissions.IsAuthenticated(), IsAccountOwner(),)

def create(self, request):
serializer = self.serializer_class(data=request.data)

if serializer.is_valid():
Account.objects.create_user(**serializer.validated_data)

return Response(serializer.validated_data, status=status.HTTP_201_CREATED)

return Response({
'status': 'Bad request',
'message': 'Account could not be created with received data.'
}, status = status.HTTP_400_BAD_REQUEST)

最佳答案

您必须创建该 IndexView 并将其导入您的 urls.py。目前解释器提示,因为在 urls.py IndexView 是未知的。要创建一个新 View ,您应该在 views.py 中创建一个新类,类似于:

from django.views.generic.base import TemplateView

class IndexView(TemplateView):
template_name = 'index.html'

ps:请阅读 Django 官方文档,非常好!

关于django,名称 'IndexView' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31287331/

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