Python Django-在类泛型视图中获取语法错误
现在我正在学习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
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
。