ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

10.返回404错误

10.返回404错误

1、修改detail视图函数

# polls/views.py
from django.shortcuts import render, get_object_or_404
def detail(request, question_id):question = get_object_or_404(Question, pk=question_id)return render(request, "polls/detail.html", {"question": question})

2、添加detail页面

# polls/templates/polls/detail.html
<h1>{{ question.question_text }}</h1>
<ul>{% for choice in question.choice_set.all %}<li>{{ choice.choice_text }}</li>{% endfor %}
</ul>

4、访问存在的问题测试
image

3、访问不存在的问题测试
image

返回列表