【问题标题】:django, name 'IndexView' is not defineddjango,未定义名称“IndexView”
【发布时间】:2015-07-08 08:28:26
【问题描述】:

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

编辑:

我的意见.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)

【问题讨论】:

  • 您是否声明了其他任何 main 视图?显示您的文件夹树和views.py 模块。
  • @很快我编辑了我的问题

标签: django django-urls


【解决方案1】:

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

from django.views.generic.base import TemplateView

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

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

【讨论】:

【解决方案2】:

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

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)

【讨论】:

    【解决方案3】:

    在你的 urls.py 中

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

    (.views 或 yourProject.views)

    在您的意见中.py 做 daveoncode 写的事情

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 1970-01-01
      • 2022-06-15
      • 2022-10-16
      • 2018-07-14
      • 2017-12-25
      • 2022-01-25
      • 2021-07-17
      • 2012-05-13
      相关资源
      最近更新 更多