Django-05,URL跳转、通用视图ListView、DetailView、改良URLConf、csrf

参考官方文档

开发工具及环境

PyCharm,Django5.2.6,Python3.11

正文

编写 detail.html 的表单

提交方法是 post,“{% csrf_token %}”则是防止跨站点请求伪造,是 Django 自带的防御功能。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<form
	action="{% url 'polls:vote' question.id %}"
	method="post"
>
	{% csrf_token %}
	<fieldset>
		<legend><h1>{{ question.question_text }}</h1></legend>
		{% if error_message %}
		<p><strong>{{ error_message }}</strong></p>
		{% endif %} {% for choice in question.choice_set.all %}
		<input
			type="radio"
			name="choice"
			id="choice{{ forloop.counter }}"
			value="{{ choice.id }}"
		/>
		<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label
		><br />
		{% endfor %}
	</fieldset>
	<input
		type="submit"
		value="Vote"
	/>
</form>

编写 vote 视图

HttpResponseRedirect 是重定向,重定向到 polls:results

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django.db.models import F
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from .models import Choice, Question

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST["choice"])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        return render(
            request,
            "polls/detail.html",
            {
                "question": question,
                "error_message": "You didn't select a choice.",
            },
        )
    else:
        selected_choice.votes = F("votes") + 1
        selected_choice.save()
        return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))

通用视图 DetailView

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
view.py改为
class IndexView(generic.ListView):
    template_name = "polls/index.html"
    context_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"


class ResultsView(generic.DetailView):
    model = Question
    template_name = "polls/results.html"

通用视图 ListView

使用 DetailView 通用视图来替换我们的 detail() 和 results() 视图,它期望从 URL 中捕获的主键值被称为 “pk”。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
urls.py 
urlpatterns = [
    # ex: /polls/ 将根路径("")映射到views模块的index函数,并命名为"index"
    path("", views.index, name="index"),
    # ex: /polls/5/
    path("<int:question_id>/", views.detail, name="detail"),
    # ex: /polls/5/results/
    path("<int:question_id>/results/", views.results, name="results"),
    # ex: /polls/5/vote/
    path("<int:question_id>/vote/", views.vote, name="vote"),
]
改为
app_name = "polls"
urlpatterns = [
    path("", views.IndexView.as_view(), name="index"),
    path("<int:pk>/", views.DetailView.as_view(), name="detail"),
    path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
    path("<int:question_id>/vote/", views.vote, name="vote"),
]
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计