Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-在类泛型视图中获取语法错误_Python_Django_View_Syntax - Fatal编程技术网

Python Django-在类泛型视图中获取语法错误

Python Django-在类泛型视图中获取语法错误,python,django,view,syntax,Python,Django,View,Syntax,现在我正在学习django并查阅文档。当我尝试使用通用视图时,它给了我一个例外: File "/home/jeffr/Рабочий стол/codetry/mysite1/polls/views.py", line 8 def IndexView(generic.ListView): ^ SyntaxError: invalid syntax 这是我的观点。py: from django.views import

现在我正在学习django并查阅文档。当我尝试使用通用视图时,它给了我一个例外:

  File "/home/jeffr/Рабочий стол/codetry/mysite1/polls/views.py", line 8
     def IndexView(generic.ListView):
                          ^
  SyntaxError: invalid syntax
这是我的观点。py:

    from django.views import generic

    from .models import Choice, Question

    def IndexView(generic.ListView):
        template_name = 'polls/index.html'
         contest_object_name = 'latest_question_list'

    get_queryset(self):
        """Return the last five published questions"""
        return Question.objects.order_by('-pub_date')[:5]

    def DetailView(generic.DetailView):
        model = Question
        template_name = 'polls/detail.html'
可在此处找到完整的回溯粘贴:


任何帮助都将不胜感激,谢谢关键字
def
表示您正在实现一项功能。但这里您并不是指定一个函数,而是指定一个类。您可以使用
class
关键字定义一个类,如:

from django.views import generic

from .models import Choice, Question

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
     contest_object_name = 'latest_question_list'

    def get_queryset(self):
        """Return the last five published questions"""
        return Question.objects.order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'
从django.views导入泛型
模型导入选择,问题
类索引视图(generic.ListView):
模板名称='polls/index.html'
竞赛对象名称='最新问题列表'
def get_queryset(自我):
“”“返回最后五个已发布的问题”“”
返回问题.objects.order_by('-pub_date')[:5]
类详细视图(generic.DetailView):
模型=问题
模板名称='polls/detail.html'
这会引发一个错误,因为函数可以接受参数,但参数名称不能包含点


您还忘了使用
def
来定义
get\u queryset
方法。

如果定义*函数,则应使用
class IndexView(…)
而不是
def IndexView