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

posted @ 2025-12-24 11:27  省时哥  阅读(1)  评论(0)    收藏  举报