django,名称 'IndexView' 未定义
django, name 'IndexView' is not defined
我正在关注 this tutorial. At the moment I am at 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
。如果你们能给我一些关于这方面的建议,那就太好了。
编辑:
我的views.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
中创建一个新的 class,例如:
from django.views.generic.base import TemplateView
class IndexView(TemplateView):
template_name = 'index.html'
ps:请阅读Django官方文档,非常好!
在你的 urls.py
from .views import IndexView
url('^.*$', IndexView.as_view(), name='index'),
(.views 或 yourProject.views)
在你的 views.py
做 daveoncode 写的
IndexView class 在样板项目的视图文件中。
C:\...\thinkster-django-angular-boilerplate-release\thinkster_django_angular_boilerplate\views
将该内容复制并粘贴到项目的视图文件中。
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic.base import TemplateView
from django.utils.decorators import method_decorator
class IndexView(TemplateView):
template_name = 'index.html'
@method_decorator(ensure_csrf_cookie)
def dispatch(self, *args, **kwargs):
return super(IndexView, self).dispatch(*args, **kwargs)
我正在关注 this tutorial. At the moment I am at 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
。如果你们能给我一些关于这方面的建议,那就太好了。
编辑:
我的views.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
中创建一个新的 class,例如:
from django.views.generic.base import TemplateView
class IndexView(TemplateView):
template_name = 'index.html'
ps:请阅读Django官方文档,非常好!
在你的 urls.py
from .views import IndexView
url('^.*$', IndexView.as_view(), name='index'),
(.views 或 yourProject.views)
在你的 views.py 做 daveoncode 写的
IndexView class 在样板项目的视图文件中。
C:\...\thinkster-django-angular-boilerplate-release\thinkster_django_angular_boilerplate\views
将该内容复制并粘贴到项目的视图文件中。
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic.base import TemplateView
from django.utils.decorators import method_decorator
class IndexView(TemplateView):
template_name = 'index.html'
@method_decorator(ensure_csrf_cookie)
def dispatch(self, *args, **kwargs):
return super(IndexView, self).dispatch(*args, **kwargs)