{"id":7414,"date":"2023-04-16T17:25:16","date_gmt":"2023-04-16T09:25:16","guid":{"rendered":"https:\/\/www.5x44.cn\/?p=7414"},"modified":"2023-04-16T17:25:16","modified_gmt":"2023-04-16T09:25:16","slug":"%e7%ac%ac%e4%b8%80%e4%b8%aadjango%e5%ba%94%e7%94%a8part4","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=7414","title":{"rendered":"\u7b2c\u4e00\u4e2adjango\u5e94\u7528[part4]"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5199\u4e00\u4e2a\u6700\u5c0f\u7684\u8868\u5355<\/h2>\n\n\n\n<p>\u66f4\u65b0part3\u4e2d\u7684detail\u6a21\u7248\uff08polls\/templates\/polls\/detail.html\uff09\uff0c\u8ba9\u5176\u5305\u542b\u8868\u5355&lt;form&gt;\u5143\u7d20\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"{% url 'polls:vote' question.id %}\" method=\"post\"&gt;\n{% csrf_token %}\n&lt;fieldset&gt;\n    &lt;legend&gt;&lt;h1&gt;{{ question.question_text }}&lt;\/h1&gt;&lt;\/legend&gt;\n    {% if error_message %}&lt;p&gt;&lt;strong&gt;{{ error_message }}&lt;\/strong&gt;&lt;\/p&gt;{% endif %}\n    {% for choice in question.choice_set.all %}\n        &lt;input type=\"radio\" name=\"choice\" id=\"choice{{ forloop.counter }}\" value=\"{{ choice.id }}\"&gt;\n        &lt;label for=\"choice{{ forloop.counter }}\"&gt;{{ choice.choice_text }}&lt;\/label&gt;&lt;br&gt;\n    {% endfor %}\n&lt;\/fieldset&gt;\n&lt;input type=\"submit\" value=\"Vote\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<p>\u5feb\u901f\u7eb2\u8981\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0a\u9762\u7684\u6a21\u7248\u663e\u793a\u4e86\u4e00\u4e2a\u4e3a\u6bcf\u4e2a\u95ee\u9898\u9009\u9879\u7684radio\u6309\u94ae\u3002radio\u6309\u94ae\u7684<strong>\u503c<\/strong>\u662f\u5173\u8054\u5230\u9009\u62e9\u95ee\u9898\u7684<strong>id<\/strong>\u4e0a\u3002\u6bcf\u4e2aradio\u6309\u94ae\u7684\u540d\u5b57\u90fd\u662f&#8221;choice\u201d\uff0c\u8fd9\u610f\u5473\u7740\u5f53\u6709\u4eba\u9009\u62e9\u4e86\u4e00\u4e2aradio\u6309\u94ae\u5e76\u70b9\u51fb\u4e86\u8868\u5355\u7684\u201c\u63d0\u4ea4\u201d\uff0c\u5b83\u4f1a\u53d1\u9001POST\u6570\u636e<strong>choice=#<\/strong>\uff0c\u8fd9\u91cc\u7684<strong>#<\/strong>\u662f\u5df2\u7ecf\u9009\u62e9\u7684ID\u3002<em>\u8fd9\u5c5e\u4e8e\u57fa\u7840\u7684HTML\u8868\u5355\u6982\u5ff5<\/em>\u3002<\/li>\n\n\n\n<li>\u8bbe\u7f6e\u8868\u5355\u7684<strong>action<\/strong>\u5230<strong>{% url &#8216;polls:vote&#8217; question.id %}<\/strong>\uff0c\u5e76\u4e14\u8bbe\u7f6e<strong>method=&#8221;post&#8221;<\/strong>\u3002\u4f7f\u7528<strong>method=&#8221;post&#8221;<\/strong>\uff08\u4e0e\u5176\u76f8\u5bf9\u5e94\u7684\u662f<strong>metthod=&#8221;get&#8221;<\/strong>\uff09<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u975e\u5e38\u91cd\u8981<\/mark><\/strong>\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u8ba9\u63d0\u4ea4\u7684\u6570\u636e\u5728\u670d\u52a1\u5668\u7aef\u4fee\u6539\u3002 \u65e0\u8bba\u4f55\u65f6\u53ea\u8981\u6570\u636e\u662f\u5728\u670d\u52a1\u5668\u7aef\u4fee\u6539\uff0c\u5c31\u8981\u4f7f\u7528<strong>method=&#8221;post&#8221;<\/strong>\u3002\u8fd9\u4e0d\u4ec5\u4ec5\u662fdjango\u8fd9\u4e48\u505a\uff0c\u6240\u6709\u597d\u7684web\u5f00\u53d1\u90fd\u8fd9\u4e48\u5e72\u3002<\/li>\n\n\n\n<li>forloop.counter\u6307\u51fafor\u6807\u7b7e\u5df2\u7ecf\u5faa\u73af\u4e86\u591a\u5c11\u6b21\u3002<\/li>\n\n\n\n<li>\u56e0\u4e3a\u521b\u5efa\u7684\u662fPOST\u8868\u5355\uff0c\u6240\u4ee5\u9700\u8981\u62c5\u5fc3CSRF\u653b\u51fb\u3002\u4e07\u5e78\u7684\u662fdjango\u6709\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u9632\u5fa1\u7cfb\u7edf\uff0c\u53ea\u8981\u7b80\u5355\u7684\u4f7f\u7528<strong>{% csrf_token %}<\/strong>\u6a21\u7248\u6807\u7b7e\u5c31\u53ef\u4ee5\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u73b0\u5728\u521b\u5efa\u4e00\u4e2a\u89c6\u56fe\u5904\u7406\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u4fee\u6539polls\/views.py\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.http import HttpResponse, HttpResponseRedirect\nfrom django.shortcuts import get_object_or_404, render\nfrom django.urls import reverse\n\nfrom .models import Choice, Question\n\n\n# ...\ndef vote(request, question_id):\n    question = get_object_or_404(Question, pk=question_id)\n    try:\n        selected_choice = question.choice_set.get(pk=request.POST&#91;\"choice\"])\n    except (KeyError, Choice.DoesNotExist):\n        # Redisplay the question voting form.\n        return render(\n            request,\n            \"polls\/detail.html\",\n            {\n                \"question\": question,\n                \"error_message\": \"You didn't select a choice.\",\n            },\n        )\n    else:\n        selected_choice.votes += 1\n        selected_choice.save()\n        # Always return an HttpResponseRedirect after successfully dealing\n        # with POST data. This prevents data from being posted twice if a\n        # user hits the Back button.\n        return HttpResponseRedirect(reverse(\"polls:results\", args=<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><strong>(<\/strong><\/mark>question.id<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">,<\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">)<\/mark><\/strong>))<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5305\u542b\u4e86\u4e0b\u9762\u51e0\u70b9\u4e4b\u524d\u8fd8\u6ca1\u6709\u8bf4\u8fc7\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>request.POST<\/strong>\uff1a\u662f\u4e00\u4e2a\u7c7b\u4f3c\u5b57\u5178\u7684\u5bf9\u8c61\uff0c\u5b83\u8ba9\u4f60\u901a\u8fc7\u5173\u952e\u5b57\u540d\u8bbf\u95ee\u88ab\u63d0\u4ea4\u7684\u6570\u636e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2drequest.POST[&#8216;choice&#8217;]\u8fd4\u56de\u4e00\u4e2aID\uff0crequest.POST\u4e00\u76f4\u8fd4\u56de\u5b57\u7b26\u578b\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>request.POST[&#8216;choice&#8217;]<\/strong>\uff1a\u5982\u679c\u2018choice\u2019\u4e0d\u5728POST\u6570\u636e\u4e2d\u4f1a\u629b\u51fa\u4e00\u4e2a<strong>KeyError<\/strong>\u9519\u8bef\u5f02\u5e38\u3002\u672c\u4f8b\u4e2d\u5982\u679cchoice\u4e0d\u5b58\u5728\uff0c\u4f1a\u91cd\u65b0\u663e\u793a\u8868\u5355\uff0c\u5e76\u63d0\u793a\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u4e86HttpResponseRedirect\u800c\u4e0d\u662fHttpResponse\u3002HttpResponseRedirect\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff1a\u7528\u6237\u5c06\u91cd\u5b9a\u5411\u5230\u7684URL\u3002<\/li>\n\n\n\n<li><strong>reverse()<\/strong>\u51fd\u6570\uff1a\u5728\u89c6\u56fe\u4e2d\u52a8\u6001\u6784\u5efaURL\u3002\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u53c2\u6570\u662f\u89c6\u56fe\u540d\uff08\u5728url.py\u4e2d\u5b9a\u4e49\uff09\uff0c\u7b2c\u4e8c\u4e2a\u5173\u952e\u5b57\u53c2\u6570args\u662f\u7528\u6765\u4f20\u9012\u4f4d\u7f6e\u53c2\u6570\u7ed9\u89c6\u56fe\uff08<strong>args\u9700\u8981\u7684\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u6240\u4ee5\u8981\u52a0\u9017\u53f7<\/strong>\uff09\u3002\u5728\u672c\u4f8b\u4e2d\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7c7b\u4f3c\u201c<em>\/polls\/3\/results<\/em>\u201d\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u91cc\u76843\u5c31\u662fquestion.id\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5f53\u6709\u4eba\u70b9\u4e86\u63d0\u4ea4\u540e\uff0cvote()\u89c6\u56fe\u4f1a\u91cd\u5b9a\u5411\u5230results\u9875\uff0c\u91cd\u5199results\u89c6\u56fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.shortcuts import get_object_or_404, render\r\n\r\n\r\ndef results(request, question_id):\r\n    question = get_object_or_404(Question, pk=question_id)\r\n    return render(request, \"polls\/results.html\", {\"question\": question})<\/code><\/pre>\n\n\n\n<p>polls\/results.html\u6a21\u7248\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h1>{{ question.question_text }}&lt;\/h1>\r\n\r\n&lt;ul>\r\n{% for choice in question.choice_set.all %}\r\n    &lt;li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}&lt;\/li>\r\n{% endfor %}\r\n&lt;\/ul>\r\n\r\n&lt;a href=\"{% url 'polls:detail' question.id %}\">Vote again?&lt;\/a><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u4e00\u822c\u89c6\u56fe\uff1a\u66f4\u5c11\u7684\u4ee3\u7801\u66f4\u597d<\/h2>\n\n\n\n<p>\u4e4b\u524d\u7684detail()\u3001results()\u548cindex()\u90fd\u662f\u7b80\u5355\u7684\u6839\u636eURL\u4ece\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u6570\u636e\u4f20\u9001\u5230\u76f8\u5e94\u7684\u6a21\u7248\u4e2d\uff0c\u52a0\u8f7d\u6a21\u7248\u5e76\u4f20\u56de\u6e32\u67d3\u597d\u7684\u6a21\u7248\u3002\u56e0\u4e3a\u8fd9\u662f\u5f88\u5e38\u7528\u7684\u5de5\u80fddjango\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u65b9\u5f0f\uff0c\u88ab\u53eb\u505a\u201c\u201d\u4e00\u822c\u89c6\u56fe\u201c\u7cfb\u7edf\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u8f6c\u6362\u6211\u4eec\u7684poll\u5e94\u7528\u4f7f\u7528\u4e00\u822c\u89c6\u56fe\uff0c\u73b0\u5728\u5fc5\u987b\u505a\u5982\u4e0b\u6b65\u9aa4\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1\u3001\u8f6c\u6362URLconf\n2\u3001\u5220\u9664\u65e7\u89c6\u56fe\n3\u3001\u4f7f\u7528\u65b0\u7684\u4e00\u822c\u89c6\u56fe<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4fee\u6539URLconf<\/h2>\n\n\n\n<p>from django.urls import path<\/p>\n\n\n\n<p>from . import views<\/p>\n\n\n\n<p>app_name = &#8220;polls&#8221;<br>urlpatterns = [<br>path(&#8220;&#8221;, views.IndexView.as_view(), name=&#8221;index&#8221;),<br>path(&#8220;\/&#8221;, views.DetailView.as_view(), name=&#8221;detail&#8221;),<br>path(&#8220;\/results\/&#8221;, views.ResultsView.as_view(), name=&#8221;results&#8221;),<br>path(&#8220;\/vote\/&#8221;, views.vote, name=&#8221;vote&#8221;),<br>]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4fee\u6539\u89c6\u56fe<\/h2>\n\n\n\n<p>from django.http import HttpResponseRedirect<br>from django.shortcuts import get_object_or_404, render<br>from django.urls import reverse<br>from django.views import generic<\/p>\n\n\n\n<p>from .models import Choice, Question<\/p>\n\n\n\n<p>class IndexView(generic.ListView):<br>template_name = &#8220;polls\/index.html&#8221;<br>context_object_name = &#8220;latest_question_list&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def get_queryset(self):\n    \"\"\"Return the last five published questions.\"\"\"\n    return Question.objects.order_by(\"-pub_date\")&#91;:5]<\/code><\/pre>\n\n\n\n<p>class DetailView(generic.DetailView):<br>model = Question<br>template_name = &#8220;polls\/detail.html&#8221;<\/p>\n\n\n\n<p>class ResultsView(generic.DetailView):<br>model = Question<br>template_name = &#8220;polls\/results.html&#8221;<\/p>\n\n\n\n<p>def vote(request, question_id):<br>\u2026 # same as above, no changes needed.<\/p>\n\n\n\n<p>\u4e0a\u9762\u4f7f\u7528\u4e86\u4e24\u4e2a\u4e00\u822c\u89c6\u56fe\uff1a<strong>ListView<\/strong>\u548c<strong>DetailView<\/strong>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u4e2a\u4e00\u822c\u89c6\u56fe\u9700\u8981\u77e5\u9053\u5b83\u64cd\u4f5c\u5728\u54ea\u4e2a\u6570\u636e\u5e93\u6a21\u578b\u4e0a\uff0c\u8fd9\u901a\u8fc7\u4f7f\u7528<strong>model<\/strong>\u5c5e\u6027\u63d0\u4f9b\u3002<\/li>\n\n\n\n<li><strong>DetailView<\/strong>\u4eceURL\u4e2d\u83b7\u5f97\u4e3b\u952e\u503c\u2014\u2014\u201d<strong>pk<\/strong>\u201c\uff0c\u6240\u4ee5\u9700\u8981\u5728URLconf\u4e2d\u6539\u53d8question_id\u5230pk\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cDetailView\u4f7f\u7528\u7684\u6a21\u7248\u88ab\u547d\u540d\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&lt;app name>\/&lt;mode name>_detail.html<\/strong><\/pre>\n\n\n\n<p>\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\u5b83\u4f1a\u4f7f\u7528\u6a21\u7248&#8221;<strong>polls\/question_detail.html<\/strong>&#8220;\u3002\u800c<strong>template_name<\/strong>\u5c5e\u6027\u7528\u4e8e\u544a\u8bc9django\u4f7f\u7528\u6307\u5b9a\u7684\u6a21\u7248\u540d\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684\u3002<\/p>\n\n\n\n<p>\u5728\u4e4b\u524d\u7684\u6559\u7a0b\u4e2d\u6a21\u7248\u90fd\u88ab\u63d0\u4f9b\u4e86\u4e00\u4e2a<strong>\u4e0a\u4e0b\u6587<\/strong>\uff0c\u5b83\u5305\u542b\u4e86<strong>question<\/strong>\u548c<strong>latest_question_list<\/strong>\u4e0a\u4e0b\u6587\u53d8\u91cf\u3002\u5bf9\u4e8e<strong>DetailView<\/strong>\u7684<strong>question<\/strong>\u53d8\u91cf\u662f\u81ea\u52a8\u751f\u6210\u7684\u2014\u2014\u56e0\u4e3a\u4f7f\u7528\u4e86Django\u6a21\u578b\uff08<strong>Question<\/strong>\uff09\uff0cDjango\u80fd\u5224\u65ad\u9002\u5408\u7684\u540d\u5b57\u7528\u4e8e\u4e0a\u4e0b\u6587\u3002\u4f46\u662fListView\u81ea\u52a8\u751f\u6210\u7684\u4e0a\u4e0b\u6587\u53d8\u91cf\u662fquestion_list\u3002\u6211\u4eec\u60f3\u4f7f\u7528<strong>latest_question_list<\/strong>\u4ee3\u66ff\uff0c\u6240\u4ee5\u4f7f\u7528\u4e86<strong>context_object_name<\/strong>\u5c5e\u6027\u6765\u8986\u76d6\u9ed8\u8ba4\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5199\u4e00\u4e2a\u6700\u5c0f\u7684\u8868\u5355 \u66f4\u65b0part3\u4e2d\u7684detail\u6a21\u7248\uff08polls\/templates\/polls\/detail.html\uff09\uff0c\u8ba9\u5176\u5305\u542b\u8868\u5355&lt;form&gt;&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=7414\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,15],"tags":[],"class_list":["post-7414","post","type-post","status-publish","format-standard","hentry","category-django","category-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7414"}],"version-history":[{"count":4,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7414\/revisions"}],"predecessor-version":[{"id":7421,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7414\/revisions\/7421"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}