Django通用视图需要使用decorator登录名

Django通用视图需要使用decorator登录名,django,Django,我是django的新手,我在django project.com上完成了由4部分组成的教程 我的问题是我想在我的投票应用程序上进行登录验证。我使用了decorator@login\u required,它工作正常,但在my views.py下,我只有vote()方法 “polls文件夹”下的my views.py “polls文件夹”下的my urls.py 来自django.conf.url.defaults导入模式,包括url 从django.views.generic导入详细视图,List

我是django的新手,我在django project.com上完成了由4部分组成的教程

我的问题是我想在我的投票应用程序上进行登录验证。我使用了decorator@login\u required,它工作正常,但在my views.py下,我只有vote()方法

“polls文件夹”下的my views.py

“polls文件夹”下的my urls.py

来自django.conf.url.defaults导入模式,包括url
从django.views.generic导入详细视图,ListView
从polls.models导入Poll
urlpatterns=模式(“”,
url(r'^$',
ListView.as_视图(
queryset=Poll.objects.order_by('-pub_date')[:5],
context\u object\u name='latest\u poll\u list',
模板(name='polls/index.html'),name='poll\u list'),
url(r'^(?P\d+/$),
DetailView.as\u视图(
模型=投票,
模板(name='polls/detail.html'),name='poll\u details'),
url(r'^(?P\d+)/results/$”,
DetailView.as\u视图(
模型=投票,
模板名称='polls/results.html'),名称='poll\u results'),
url(r'^(?P\d+)/vote/$,“polls.views.vote”),
)
在my urls.py下,我使用了通用视图

问题是:我将如何把登录要求下的民意调查应用程序的“索引”。这样,用户将首先登录,然后才能查看可用的投票

目前的情况是:我已经在my views.py和method vote()下使用了login required,投票后需要登录

有人能帮我吗

谢谢,
贾斯汀第1次进近

在URL.py中:

urlpatterns = patterns('',
    url(r'^$',
        login_required(ListView.as_view(
            queryset = Poll.objects.order_by('-pub_date')[:5],
            context_object_name = 'latest_poll_list',
            template_name = 'polls/index.html'), name='poll_lists')),
)
第二次进近

在views.py中:

class IndexView(ListView):
    queryset = Poll.objects.order_by('-pub_date')[:5]
    context_object_name = 'latest_poll_list'
    template_name = 'polls/index.html'

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):        
        return super(IndexView, self).dispatch(request, *args, **kwargs)
然后在url.py中

urlpatterns = patterns('',
        url(r'^$',
            IndexView.as_view(), name='poll_lists'),
    )

只是提供了一个可能更为最新的答案

我会将它们移动到视图文件并使用


呈现时捕获导入错误:无法导入名称列表视图,因为您在
views.py
中获得了
IndexView
,因此需要将此类导入到
url.py
。在template/home/justin/Workspace/training/django/sample/mysite/templates/base.html中的polls.views import IndexView中,将此行置于
urls.py
的开头,在呈现时在第21行发现导入错误:无法导入名称ListVieiw
  • Typo
    ListVieiw
    ->
    ListView
    class IndexView(ListView):
        queryset = Poll.objects.order_by('-pub_date')[:5]
        context_object_name = 'latest_poll_list'
        template_name = 'polls/index.html'
    
        @method_decorator(login_required)
        def dispatch(self, request, *args, **kwargs):        
            return super(IndexView, self).dispatch(request, *args, **kwargs)
    
    urlpatterns = patterns('',
            url(r'^$',
                IndexView.as_view(), name='poll_lists'),
        )
    
    from django.views.generic import (
        ListView,
        DetailView
    )
    from django.contrib.auth.mixins import LoginRequiredMixin
    
    
    class PollsListView(LoginRequiredMixin, ListView):
        model = Poll
        template_name = 'polls/index.html'