django,未定义名称'IndexView' [英] django, name 'IndexView' is not defined
问题描述
我正在关注本教程。目前我在这个点,但是当我开始我的服务器与 python manage.py runserver 0.0.0.0:8000
并打开浏览器中的URL,我收到以下错误:
I am following this tutorial. At the moment I am at this point but when I start my server with python manage.py runserver 0.0.0.0:8000
and open the url in my browser, I receive following Error:
name 'IndexView' is not defined
是我的urls.py
This is my 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
某处。如果你们可以给我一些关于这一点的建议,那将是非常棒的。
I don't know how to solve this problem, since I never saw myself even declaring this IndexView
somewhere. It would be awesome if you guys could give me some suggestions on this one.
编辑:
我的观点.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
中是未知的。
要创建新视图,您应该在 views.py
中创建一个新类,如下所示:
You have to create that IndexView
and import it in your urls.py
.
Currently the interpreter complains since in the urls.py
IndexView
is unknown.
To create a new view you should create a new class in views.py
, something like:
from django.views.generic.base import TemplateView
class IndexView(TemplateView):
template_name = 'index.html'
ps:请阅读官方的Django文档,这是非常好的!
ps: please read the official Django docs, which is very good!
这篇关于django,未定义名称'IndexView'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!