django,名称'IndexView'未定义

时间:2015-07-08 08:28:27

标签: django django-urls

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

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。如果你们能给我一些关于这个的建议,那真是太棒了。

编辑:

my 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)

3 个答案:

答案 0 :(得分:4)

您必须创建IndexView并将其导入urls.py。 目前,口译员抱怨,因为urls.py IndexView未知。 要创建新视图,您应该在views.py中创建一个新类,例如:

from django.views.generic.base import TemplateView

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

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

答案 1 :(得分:2)

IndexView类位于样板项目的视图文件中。

C:\ ... \ thinkster-Django的角样板释放\ thinkster_django_angular_boilerplate \观点

将该内容复制并粘贴到项目的视图文件中。

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)

答案 2 :(得分:1)

在你的urls.py

from .views import IndexView
url('^.*$', IndexView.as_view(), name='index'),

(。views或yourProject.views)

在你的views.py中

做什么daveoncode写的