加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

django,名称’IndexView’未定义

发布时间:2020-12-20 11:44:08 所属栏目:Python 来源:网络整理
导读:我正在关注 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,urlfrom
我正在关注 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.如果你们能给我一些关于这个的建议,那真是太棒了.

编辑:

我的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中创建一个新类,如:

from django.views.generic.base import TemplateView

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读