{"id":7405,"date":"2023-04-15T16:03:47","date_gmt":"2023-04-15T08:03:47","guid":{"rendered":"https:\/\/www.5x44.cn\/?p=7405"},"modified":"2023-04-16T10:18:26","modified_gmt":"2023-04-16T02:18:26","slug":"%e7%ac%ac%e4%b8%80%e4%b8%aadjango%e5%ba%94%e7%94%a8part3","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=7405","title":{"rendered":"\u7b2c\u4e00\u4e2aDjango\u5e94\u7528[part3]"},"content":{"rendered":"\n<p>django\u4e2dweb\u9875\u9762\u548c\u5176\u5b83\u5185\u5bb9\u90fd\u662f\u901a\u8fc7\u89c6\u56fe\uff08view\uff09\u6765\u4f20\u9012\u7684\u3002\u6bcf\u4e00\u4e2a\u89c6\u56fe\u90fd\u662f\u4e00\u4e2apython\u51fd\u6570\uff08\u6216\u65b9\u6cd5\uff09\uff0cdjango\u901a\u8fc7\u68c0\u67e5url\u7684\u8bf7\u9009\u62e9\u89c6\u56fe\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2aURL\u7684\u4e00\u822c\u5f62\u5f0f\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/newsarchive\/&lt;year&gt;\/&lt;month&gt;\/<\/code><\/pre>\n\n\n\n<p>\u4eceurl\u5230\u89c6\u56fe\u7684\u8f6c\u6362\uff0cdjango\u4f7f\u7528\u4e86\u2018<strong>URLconfs<\/strong>\u2019\u3002\u5b83\u662fURL\u548c\u89c6\u56fe\u7684\u6865\u6881\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5199\u66f4\u591a\u7684\u89c6\u56fe<\/h2>\n\n\n\n<p>\u5728polls\/views.py\u586b\u52a0\u4e00\u4e9b\u65b0\u89c6\u56fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def detail(request, question_id):\n    return HttpResponse(\"You're looking at question %s.\" % question_id)\n\n\ndef results(request, question_id):\n    response = \"You're looking at the results of question %s.\"\n    return HttpResponse(response % question_id)\n\n\ndef vote(request, question_id):\n    return HttpResponse(\"You're voting on question %s.\" % question_id)<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5728polls.urls\u4e2d\u52a0\u5165\u8fd9\u4e9b\u89c6\u56fe\u7684\u6620\u5c04\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.urls import path\n\nfrom . import views\n\nurlpatterns = &#91;\n    # ex: \/polls\/\n    path(\"\", views.index, name=\"index\"),\n    # ex: \/polls\/5\/\n    path(\"&lt;int:question_id&gt;\/\", views.detail, name=\"detail\"),\n    # ex: \/polls\/5\/results\/\n    path(\"&lt;int:question_id&gt;\/results\/\", views.results, name=\"results\"),\n    # ex: \/polls\/5\/vote\/\n    path(\"&lt;int:question_id&gt;\/vote\/\", views.vote, name=\"vote\"),\n]<\/code><\/pre>\n\n\n\n<p>\u4e4b\u540e\u5c31\u53ef\u4ee5\u8fd0\u884c\u670d\u52a1\u5668\u6d4b\u8bd5\u4e00\u4e0b&#8221;\/polls\/34\/&#8221;\uff0c\u5b83\u4f1a\u8fd0\u884cdetail()\u65b9\u6cd5 \u3002\u5177\u4f53\u8fc7\u7a0b\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<p><em>\u5f53\u8bf7\u6c42\u8be5\u9875\u9762\u65f6\uff0cdjango\u4f1a\u52a0\u8f7d<strong>mysite.urls<\/strong>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u88ab\u6307\u5411<strong>ROOT_URLCONF<\/strong>\u8bbe\u7f6e\u3002\u5b83\u4f1a\u67e5\u627e\u5e76\u6309\u5e8f\u904d\u5386<strong>urlpatterns<\/strong>\u8fd9\u4e2a\u53d8\u91cf\u3002\u5728\u627e\u5230\u2018polls\/&#8217;\u540e\uff0c\u4ece\u5b57\u7b26\u4e32\u4e2d\u53bb\u9664\u6389\u5339\u914d\u5230\u7684<strong>&#8220;polls&#8221;<\/strong>\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5269\u4e0b\u7684\u5b57\u7b26\u4e32<strong>&#8220;34\/&#8221;<\/strong>\u53d1\u9001\u5230<strong>&#8216;polls.urls&#8217;<\/strong>\u4e2d\u8fdb\u884c\u4e0b\u4e00\u6b65\u5904\u7406\u3002\u5728<strong>&#8216;polls.urls&#8217;<\/strong>\u4e2d\u5339\u914d\u5230&#8217;<strong>&lt;int:question_id&gt;<\/strong>\/&#8217;\uff0c\u7ed3\u679c\u8c03\u7528<strong>detail()<\/strong>\u89c6\u56fe\uff0c\u5c31\u5982\u540c\uff1a<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">detail(request=&lt;HttpRequest object>, question_id=34)<\/mark><\/code><\/pre>\n\n\n\n<p><strong>question_id<\/strong>=34\u8fd9\u90e8\u5206\u6765\u81ea<strong>&lt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">int<\/mark>:question_id&gt;<\/strong>\u3002\u4f7f\u7528\u4e86\u5c16\u62ec\u53f7\u5305\u542b\u4e86URL\u7684\u4e00\u90e8\u5206\uff0c\u5e76\u628a\u5b83\u505a\u4e3a\u5173\u952e\u5b57\u53c2\u6570\u53d1\u9001\u5230\u89c6\u56fe\u3002<strong>question_id<\/strong>\u5b9a\u4e49\u4e86\u4f1a\u88ab\u5339\u914d\u5230\u90e8\u5206\u7684\u540d\u5b57\uff0cint\u662f\u4e2a\u8f6c\u6362\u5668\uff0c\u5b83\u5224\u65ad\u4ec0\u4e48\u6a21\u5f0f\u5e94\u8be5\u5339\u914d\u8fd9\u90e8\u5206URL\uff0c\u5192\u53f7\uff08\uff1a\uff09\u5206\u5f00\u8f6c\u6362\u5668\u548c\u6a21\u5f0f\u540d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8ba9\u89c6\u56fe\u6709\u4e00\u4e9b\u5b9e\u9645\u529f\u80fd<\/h2>\n\n\n\n<p>\u6bcf\u4e2a\u89c6\u56fe\u90fd\u4f1a\u4ed8\u8d23\u505a\u4e00\u4e24\u4ef6\u4e8b\uff1a\u8981\u4e48\u8fd4\u56de\u4e00\u4e2a<strong>HttpResponse<\/strong>\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u8bf7\u6c42\u9875\u8bf7\u6c42\u7684\u5185\u5bb9\uff0c\u6216\u8005\u8fd4\u56de\u4e00\u4e2a\u201c\u4f8b\u5916\u201d\u5982<strong>Http404<\/strong>\u3002<\/p>\n\n\n\n<p>\u91cd\u5199polls\/views.py\u4e2d\u7684index()\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.http import HttpResponse\n\nfrom .models import Question\n\ndef index(request):\n    latest_question_list = Question.objects.order_by(\"-pub_date\")&#91;:5]\n    output = \", \".join(&#91;q.question_text for q in latest_question_list])\n    return HttpResponse(output)\n\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6709\u4e00\u4e2a\u95ee\u9898\uff1a\u89c6\u56fe\u4e2d\u7684\u4ee3\u7801\u662f\u56fa\u5b9a\u7684\u3002\u5982\u679c\u60f3\u6539\u53d8\u89c6\u56fe\u7684\u5916\u89c2\u5c31\u8981\u91cd\u65b0\u7f16\u5199\u8fd9\u4e2apython\u4ee3\u7801\uff0c\u6240\u4ee5\u73b0\u5728\u8ba9\u6211\u4eec\u4f7f\u7528django\u7684\u6a21\u7248\u7cfb\u7edf\uff0c\u7528\u6765\u5206\u9694\u8bbe\u8ba1\u548cpython\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u5148\u5728<strong>polls<\/strong>\u76ee\u5f55\u4e0b\u5efa\u7acb\u4e00\u4e2a\u540d\u4e3a<strong>templates<\/strong>\u6587\u4ef6\u5939\uff0cdjango\u4f1a\u5728\u8fd9\u91cc\u67e5\u627e\u6a21\u7248\u3002\u9879\u76ee\u7684<strong>TEMPLATES<\/strong>\u8bbe\u7f6e\u63cf\u8ff0\u4e86django\u5982\u4f55\u52a0\u8f7d\u548c\u8c03\u7528\u6a21\u7248\uff0c\u9ed8\u8ba4<strong>DjangoTemplates<\/strong>\u4e3a\u6a21\u7248\u540e\u7aef\uff0c\u5b83\u7684<strong>APP_DIRS<\/strong>\u9009\u9879\u8bbe\u7f6e\u4e3a<strong>True<\/strong>\uff0c\u4e60\u60ef\u4e0a<strong>DjangoTemplates<\/strong>\u4f1a\u67e5\u627e\u5e94\u7528\u4e0b\u9762\u7684&#8221;<strong>templates<\/strong>\u201c\u5b50\u76ee\u5f55\u3002<\/p>\n\n\n\n<p>\u5728\u521a\u5efa\u7acb\u7684templates\u76ee\u5f55\u91cc\u521b\u5efa\u4e00\u4e2a\u540d\u4e3apolls\u7684\u76ee\u5f55\uff0c\u5e76\u4e14\u5728\u91cc\u9762\u5efa\u7acb\u4e00\u4e2a\u540d\u4e3aindex.html\u7684\u6587\u4ef6\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6a21\u7248\u8def\u5f84\u5e94\u8be5\u662f<strong>polls\/templates\/polls\/index.html<\/strong>\uff0c\u8fd9\u65f6\u53ef\u4ee5\u5728\u9879\u76ee\u4e2d\u7528<strong>polls\/index.html<\/strong>\u7684\u65b9\u5f0f\u8c03\u7528\u8fd9\u4e2a\u6a21\u7248\u3002<\/p>\n\n\n\n<p><strong>PS<\/strong>\uff1a<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u8fd9\u6837\u53ef\u4ee5\u9632\u6b62\u540c\u540d\u6a21\u7248\u7684\u95ee\u9898\uff0c\u56e0\u4e3apolls\u6307\u660e\u4e86\u8def\u5f84\uff0c\u56e0\u4e3a\u6240\u6709\u7684\u6a21\u7248\u5171\u4eab\u4e00\u4e2a\u540d\u5b57\u7a7a\u95f4\uff0c\u5982\u679c\u76f4\u63a5\u5c06index.html\u653e\u5728templates\u4e2d\u7684\u8bdd\uff0c\u5728\u5176\u5b83\u5e94\u7528\u4e5f\u6709index.html\u6a21\u7248\u65f6\uff0cdjango\u4f1a\u65e0\u6cd5\u533a\u5206\u4ed6\u4eec<\/mark>\u3002<\/p>\n\n\n\n<p>polls\/templates\/polls\/index.html\u7684\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{% if latest_question_list %}\n    &lt;ul&gt;\n    {% for question in latest_question_list %}\n        &lt;li&gt;&lt;a href=\"\/polls\/{{ question.id }}\/\"&gt;{{ question.question_text }}&lt;\/a&gt;&lt;\/li&gt;\n    {% endfor %}\n    &lt;\/ul&gt;\n{% else %}\n    &lt;p&gt;No polls are available.&lt;\/p&gt;\n{% endif %}<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\u66f4\u65b0\u4e00\u4e0b<strong>polls\/views.py<\/strong>\u4e2d\u7684<strong>index<\/strong>\u89c6\u56fe\uff0c\u8ba9\u5176\u4f7f\u7528\u6a21\u7248\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.http import HttpResponse\nfrom django.template import loader\n\nfrom .models import Question\n\n\ndef index(request):\n    latest_question_list = Question.objects.order_by(\"-pub_date\")&#91;:5]\n    template = loader.get_template(\"polls\/index.html\")\n    context = {\n        \"latest_question_list\": latest_question_list,\n    }\n    return HttpResponse(template.render(context, request))<\/code><\/pre>\n\n\n\n<p>\u4ee3\u7801\u52a0\u8f7dpolls\/index.html\u6a21\u7248\u5e76\u4f20\u7ed9\u5b83\u4e00\u4e2a&#8221;\u5185\u5bb9&#8221;\u3002\u8fd9\u4e2a\u201d\u5185\u5bb9&#8221;\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5b83\u6620\u5c04\u4e86\u6a21\u7248\u53d8\u91cf\u5230python\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p><strong>PS:\u6a21\u7248\u4e2d\u53ef\u4ee5\u89e3\u6790\u51facontext\u7684\u5185\u5bb9\uff0c\u5728\u6a21\u7248\u4e2d\u4f7f\u7528\u5b57\u5178\u7684\u5185\u5bb9\u4f7f\u7528{{}}\u6765\u5f15\u7528\uff0ccontext\u7684key\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">{{latest_question_list}}<\/mark>\u76f8\u5f53\u4e8epython\u4ee3\u7801\u7684<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">context[&#8216;latest_question_list&#8217;]<\/mark>\u3002<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5feb\u6377\u65b9\u5f0f\uff1arender()<\/h2>\n\n\n\n<p>\u5b83\u662f\u4e2a\u975e\u5e38\u5e38\u7528\u7684\u52a0\u8f7d\u6a21\u7248\u7684\u65b9\u6cd5\uff0c\u586b\u5145\u5185\u5bb9\u5e76\u8fd4\u56de\u4e00\u4e2a\u643a\u5e26\u76f8\u5e94\u6a21\u7248\u7ed3\u679c\u7684<strong>HttpResponse<\/strong>\u5bf9\u8c61\u3002\u4f7f\u7528render()\u91cd\u5199index()\u89c6\u56fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.shortcuts import render\n\nfrom .models import Question\n\n\ndef index(request):\n    latest_question_list = Question.objects.order_by(\"-pub_date\")&#91;:5]\n    context = {\"latest_question_list\": latest_question_list}\n    return render(request, \"polls\/index.html\", context)<\/code><\/pre>\n\n\n\n<p>\u5728\u4f7f\u7528<strong>render<\/strong>\u540e\u5c31\u4e0d\u518d\u9700\u8981\u5bfc\u5165<strong>loader<\/strong>\u548c<strong>HttpResponse<\/strong>\u5934\u6587\u4ef6\u4e86\u3002\uff08\u5982\u679c\u8fd8\u60f3\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u7684\u8bdd\uff0c\u90a3\u5c31\u8981\u7ee7\u7eed\u4f7f\u7528loader\u548cHttpResponse\uff09\u3002<\/p>\n\n\n\n<p><strong>render()<\/strong>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f<strong>request<\/strong>\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f<strong>\u6a21\u7248\u540d<\/strong>\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u53ef\u9009\u7684<strong>\u5b57\u5178<\/strong>\uff0c\u7528\u6765\u4f20\u9012python\u53d8\u91cf\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u629b\u51fa404\u9519\u8bef<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.http import Http404\nfrom django.shortcuts import render\n\nfrom .models import Question\n\n\n# ...\ndef detail(request, question_id):\n    try:\n        question = Question.objects.get(pk=question_id)\n    except Question.DoesNotExist:\n        raise <strong>Http404<\/strong>(\"Question does not exist\")\n    return render(request, \"polls\/detail.html\", {\"question\": question})<\/code><\/pre>\n\n\n\n<p>\u5728question_id\u4e0d\u5b58\u5728\u65f6\uff0c\u629b\u51fa404\u9519\u8bef<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5feb\u6377\u65b9\u5f0f\uff1aget_object_or_404()<\/h2>\n\n\n\n<p>\u8fd9\u662f\u4e2a\u5e38\u7528\u4e8eget()\u548c\u629b\u51fa404\u9519\u8bef\u7684\u5feb\u6377\u65b9\u5f0f\u3002\u7528\u6b64\u65b9\u6cd5\u6539\u5199\u4e00\u4e2adetail()\u89c6\u56fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.shortcuts import get_object_or_404, render\n\nfrom .models import Question\n\n# ...\ndef detail(request, question_id):\n    question = get_object_or_404(Question, pk=question_id)\n    return render(request, \"polls\/detail.html\", {\"question\": question})<\/code><\/pre>\n\n\n\n<p><strong>get_object_or_404()<\/strong>\u51fd\u6570\u4f7f\u7528Django\u6a21\u578b\u505a\u4e3a\u5b83\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u4e4b\u540e\u662f\u4efb\u610f\u6570\u91cf\u7684\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u8fd9\u4e9b\u5173\u952e\u5b57\u53c2\u6570\u7528\u4e8e\u4f20\u9012\u7ed9\u6a21\u578b\u7ba1\u7406\u5668\u7684<strong>get(<\/strong>)\u51fd\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u6a21\u7248\u7cfb\u7edf<\/h2>\n\n\n\n<p>\u7b80\u5355\u5199\u4e00\u4e0bdetail()\u89c6\u56fe\u7684\u6a21\u7248\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h1&gt;{{ question.question_text }}&lt;\/h1&gt;\n&lt;ul&gt;\n{% for choice in question.choice_set.all %}\n    &lt;li&gt;{{ choice.choice_text }}&lt;\/li&gt;\n{% endfor %}\n&lt;\/ul&gt;<\/code><\/pre>\n\n\n\n<p>\u6a21\u7248\u4e2d\u4f7f\u7528<strong>\u201d\u70b9-\u67e5\u627e\u201c\u8bed\u6cd5\u8bbf\u95ee\u53d8\u91cf\u5c5e\u6027<\/strong>\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d{{ question.question_text }}\uff0c\u9996\u5148Django\u5728question\u5bf9\u8c61\u4e0a\u8fdb\u884c\u5b57\u5178\u67e5\u627e\u3002\u5982\u679c\u5931\u8d25\u4e86\uff0c\u5b83\u4f1a\u5c1d\u8bd5\u5c5e\u6027\u67e5\u627e\u2014\u2014\u5728\u4e0a\u9762\u7684\u4f8b\u5b57\u4e2d\u4f7f\u7528\u4e86\u5c5e\u6027\u67e5\u627e\uff0c\u5982\u679c\u5c5e\u6027\u67e5\u627e\u5931\u8d25\u4e86\uff0c\u5b83\u5c06\u5c1d\u8bd5\u5217\u8868\u7d22\u5f15\u67e5\u627e\u3002\uff08PS:\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4f60\u8981\u8bbf\u95ee\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u70b9\u53f7\u6765\u8bbf\u95ee\u5b83\u3002\u5982\u679c\u60f3\u8981\u8bbf\u95ee\u4e00\u4e2a\u5bf9\u8c61\u7684\u5b57\u5178\u952e\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u70b9\u53f7\u548c\u5b57\u5178\u952e\u540d\u6765\u8bbf\u95ee\u5b83\u3002\u5982\u679c\u8981\u8bbf\u95ee\u4e00\u4e2a\u5bf9\u8c61\u7684\u5217\u8868\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u70b9\u53f7\u548c\u5217\u8868\u5f15\u6765\u8bbf\u95ee\u5b83\u3002\uff09<\/p>\n\n\n\n<p>\u65b9\u6cd5\u8c03\u7528\u53d1\u751f\u5728<strong>{% for %}<\/strong>\u5faa\u73af\u4e2d\uff1a<strong>question.choice_set.all<\/strong>\u88ab\u89e3\u91ca\u4e3apython\u4ee3\u7801<strong>question.choice_set.all()<\/strong>\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u53ef\u8fed\u4ee3\u7684<strong>Choice<\/strong>\u5bf9\u8c61\u5e76\u4e14\u53ef\u4ee5\u7528\u4e8e{% for %}\u6807\u7b7e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u79fb\u9664\u6a21\u7248\u4e2d\u7684\u786c\u94fe\u63a5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;li&gt;&lt;a href=\"\/polls\/{{ question.id }}\/\"&gt;{{ question.question_text }}&lt;\/a&gt;&lt;\/li&gt;<\/code><\/pre>\n\n\n\n<p>\u6a21\u7248\u4e2d\u51fa\u73b0\u4e0a\u9762\u7684\u4ee3\u7801\u5c31\u662f\u786c\u94fe\u63a5\u3002\u5728\u9879\u76ee\u4e2d\u5982\u679c\u60f3\u6539\u53d8URL\uff0c\u4e0a\u9762\u7684\u786c\u94fe\u63a5\u4f1a\u4ea7\u751f\u5f88\u5927\u7684\u5de5\u4f5c\u91cf\u3002\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u5728<strong>polls.urls<\/strong>\u6a21\u5757\u4e2d<strong>path()<\/strong>\u51fd\u6570\u4f7f\u7528<strong>name<\/strong>\u53c2\u6570\uff0c\u4f1a\u6539\u53d8\u8fd9\u4e00\u5207\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528{% url %}\u6a21\u7248\u6807\u7b7e\u6765\u505a\u5230\u8fd9\u70b9\uff0c\u66f4\u65b0\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;li&gt;&lt;a href=\"{% url 'detail' question.id %}\"&gt;{{question.question_text}}&lt;\/a&gt;&lt;\/li&gt;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u65f6polls.urls\u4e2dpath()\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u53ef\u4ee5\u6539\u6210\u4efb\u4f55\u60f3\u8981\u6539\u53d8\u7684\u6a21\u5f0f\uff0c\u6bd4\u5982\u50cf\u6211\u6539\u6210\u4e86\u4ee5k\u5f00\u5934\u7684\u94fe\u63a5\uff0c\u6a21\u7248\u4e2d\u7684\u94fe\u63a5\u4e5f\u4f1a\u8ddf\u7740\u6539\u53d8\uff0c\u800c\u4e0d\u7528\u56e0\u4e3a\u4fee\u6539\u4e86url\u4e2d\u7684\u6a21\u5f0f\uff0c\u800c\u6539\u53d8\u6a21\u7248\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>path(\"<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">k\/&lt;int:question_id&gt;\/<\/mark>\", views.detail,name='detail'),<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">URL\u540d\u5b57\u7a7a\u95f4<\/h2>\n\n\n\n<p>\u7ed9url\u547d\u540d\u7684\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u6a21\u7248\u8c03\u7528\u66f4\u65b9\u4fbf\uff0c\u4f46\u662f\u5982\u679c\u6709\u591a\u4e2a\u5e94\u7528\u90fd\u6709detail\u600e\u4e48\u529e\uff1f\u90a3\u5c31\u9700\u8981\u5728URLconf\u4e2d\u914d\u7f6e\u540d\u5b57\u7a7a\u95f4\u2014\u2014<strong>app_name<\/strong>\uff1a<\/p>\n\n\n\n<p>\u5728polls\/urls.py\u4e2d\u52a0\u5165app_name\uff0c\u6307\u5b9a\u5b83\u7684\u540d\u5b57\u7a7a\u95f4\uff0c\u7136\u540e\u5728\u8c03\u7528\u7684\u6a21\u7248\u4e2d\u8fde\u540c\u540d\u5b57\u7a7a\u95f4\u4e00\u8d77\u8c03\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.urls import path\n\nfrom . import views\n\n<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">app_name = \"polls\"<\/mark><\/strong>\nurlpatterns = &#91;\n    path(\"\", views.index, name=\"index\"),\n    path(\"&lt;int:question_id&gt;\/\", views.detail, name=\"detail\"),\n    path(\"&lt;int:question_id&gt;\/results\/\", views.results, name=\"results\"),\n    path(\"&lt;int:question_id&gt;\/vote\/\", views.vote, name=\"vote\"),\n]<\/code><\/pre>\n\n\n\n<p>\u6a21\u7248\u4e2d\u505a\u5982\u4e0b\u6539\u53d8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;li&gt;&lt;a href=\"{% url 'polls:detail' question.id %}\"&gt;{{ question.question_text }}&lt;\/a&gt;&lt;\/li&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>django\u4e2dweb\u9875\u9762\u548c\u5176\u5b83\u5185\u5bb9\u90fd\u662f\u901a\u8fc7\u89c6\u56fe\uff08view\uff09\u6765\u4f20\u9012\u7684\u3002\u6bcf\u4e00\u4e2a\u89c6\u56fe\u90fd\u662f\u4e00\u4e2apython\u51fd\u6570\uff08\u6216\u65b9\u6cd5\uff09\uff0cdjango\u901a\u8fc7\u68c0\u67e5url\u7684\u8bf7\u9009\u62e9\u89c6\u56fe\u3002 \u4e00\u4e2a&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=7405\"> 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],"tags":[],"class_list":["post-7405","post","type-post","status-publish","format-standard","hentry","category-django"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7405","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=7405"}],"version-history":[{"count":7,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7405\/revisions"}],"predecessor-version":[{"id":7416,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7405\/revisions\/7416"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}