{"id":1176,"date":"2018-08-07T16:30:40","date_gmt":"2018-08-07T08:30:40","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=1176"},"modified":"2018-08-09T15:03:11","modified_gmt":"2018-08-09T07:03:11","slug":"django%e4%b8%adurlconf%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=1176","title":{"rendered":"Django\u4e2dURLconf\u7684\u57fa\u672c\u7528\u6cd5"},"content":{"rendered":"<p>\u5728DJANGO\u4e2d\uff0cWEB\u9875\u9762\u548c\u5176\u5b83\u5185\u5bb9\u90fd\u7531\u89c6\u56fe\u4ea7\u751f\u3002\u6bcf\u4e00\u4e2a\u89c6\u56fe\u90fd\u662f\u7531\u4e00\u4e2a\u7b80\u5355\u7684PYTHON\u51fd\u6570\uff08\u6216\u65b9\u6cd5\uff0c\u5728\u57fa\u4e8e\u7c7b\u7684\u89c6\u56fe\u4e2d\uff09\u63cf\u8ff0\u7684\u3002DJANGO\u5c06\u901a\u8fc7\u6d4b\u8bd5\u8bf7\u6c42\uff08REQUEST\uff09\u7684URL\u9009\u62e9\u4e00\u4e2a\u89c6\u56fe\uff08\u66f4\u7cbe\u51c6\u7684\u6765\u8bf4\u662f\u57df\u540d\u540e\u7684URL\u90e8\u5206\uff09\u3002<\/p>\n<p>URL\u6a21\u5f0f\u5f88\u7b80\u5355\uff0c\u4e00\u822c\u6a21\u5f0f\u662f\uff0c\u5982\uff1a\/newsarchive\/&lt;year&gt;\/&lt;month&gt;\/.<\/p>\n<p>\u4eceURL\u5230\u4e00\u4e2a\u89c6\u56fe\uff0cDJANGO\u4f7f\u7528&#8217;URLconfs&#8217;\u3002URLconf\u5305\u542b\u4e86URL\u6a21\u5f0f\u5230\u89c6\u56fe\u7684\u6620\u5c04\u3002<\/p>\n<h1>\u5199\u66f4\u591a\u7684\u89c6\u56fe<\/h1>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u5728polls\/views.py\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u89c6\u56fe\u3002\u8fd9\u4e9b\u89c6\u56fe\u6709\u70b9\u4e0d\u540c\uff0c\u56e0\u4e3a\u5b83\u4eec\u5e26\u6709\u53c2\u6570\uff1a<\/p>\n<pre>polls\/views.py\r\n\r\ndef detail(request, question_id):\r\n    return HttpResponse(\"You're looking at question %s.\" % question_id)\r\n\r\ndef results(request, question_id):\r\n    response = \"You're looking at the results of question %s.\"\r\n    return HttpResponse(response % question_id)\r\n\r\ndef vote(request, question_id):\r\n    return HttpResponse(\"You're voting on question %s.\" % question_id)\r\n<\/pre>\n<p>\u901a\u8fc7<code>path()<\/code>\u8fde\u63a5\u8fd9\u4e9b\u8bd5\u56fe\u5230<strong>polls.urls<\/strong>\u6a21\u5757\u4e2d:<\/p>\n<pre>polls\/urls.py\r\n\r\nfrom django.urls import path\r\n\r\nfrom . import views\r\n\r\nurlpatterns = [\r\n    # ex: \/polls\/\r\n    path('', views.index, name='index'),\r\n    # ex: \/polls\/5\/\r\n    path('\/', views.detail, name='detail'),\r\n    # ex: \/polls\/5\/results\/\r\n    path('\/results\/', views.results, name='results'),\r\n    # ex: \/polls\/5\/vote\/\r\n    path('\/vote\/', views.vote, name='vote'),\r\n]\r\n\r\n<\/pre>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165&#8221;\/polls\/34\/&#8221;\uff0c\u5b83\u4f1a\u6267\u884cdetail()\u65b9\u6cd5\uff0c\u5e76\u663e\u793a\u4f60\u5728URL\u4e2d\u63d0\u4f9b\u7684ID\u3002\u800c&#8221;\/polls\/34\/results\/&#8221;\u548c&#8221;\/polls\/34\/vote\/&#8221;\u4f1a\u5206\u522b\u8c03\u7528results\u548cvote\u9875\u9762\u3002<\/p>\n<p>\u5f53\u6709\u4eba\u4eceweb\u7ad9\u70b9\u8bf7\u6c42\u4e00\u4e2a\u9875\u9762\u65f6\uff0cDjango\u4f1a\u52a0\u8f7dmysite.urls\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u88ab<code>ROOT_URLCONF<\/code>\u8bbe\u7f6e\u3002\u5b83\u4f1a\u627e\u5230\u540d\u4e3a<code>urlpatterns<\/code>\u7684\u53d8\u91cf\uff0c\u5e76\u6309\u987a\u5e8f\u901a\u8fc7\uff08traverses\uff09\u6a21\u5f0f\u3002\u5728\u5339\u914d\u5230&#8221;polls\/&#8221;\u540e\uff0c\u5b83\u53bb\u9664\u6389\u5339\u914d\u7684\u5b57\u7b26(&#8220;polls&#8221;)\uff0c\u5e76\u628a\u5269\u4f59\u7684\u5b57\u7b26\u2014\u2014&#8221;34\/&#8221;\u53d1\u9001\u5230&#8221;polls.urls&#8221;URLconf\u6a21\u5757\u4e2d\uff0c\u7528\u4e8e\u4e4b\u540e\u7684\u5904\u7406\u3002\u8fd9\u91cc\u4f1a\u5339\u914d&#8221;<code>&lt;int:question_id&gt;\/<\/code>&#8220;\uff0c\u7ed3\u679c\u662f\u8c03\u7528detail()\u89c6\u56fe\uff0c\u8c03\u7528\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<pre>detail(request=&lt;HttpRequest object&gt;, question_id=34)\r\n<\/pre>\n<p><code>question_id=34<\/code>\u90e8\u5206\u6765\u81ea&#8221;&lt;int:question_id&gt;\/&#8221;\u3002\u4f7f\u7528\u5c16\u62ec\u53f7\u201c\u6355\u83b7\u201d\u7684URL\u90e8\u5206\uff0c\u5e76\u53d1\u9001\u5b83\u505a\u4e3a\u5173\u952e\u5b57\u53c2\u6570\u5230\u89c6\u56fe\u51fd\u6570\u3002\u201cquestion_id&gt;\u201d\u90e8\u5206\u5b57\u7b26\u4e32\u5b9a\u4e49\u7684\u540d\u5b57\u5c06\u4e8e\u8bc6\u522b\u88ab\u5339\u914d\u7684\u6a21\u5f0f\u3002\u201c&lt;int:\u201d\u90e8\u5206\u662f\u4e00\u4e2a\u8f6c\u6362\u5668\uff0c\u5b83\u51b3\u5b9a\u4e86\u4ec0\u4e48\u6837\u7684\u6a21\u5f0f\u5c06\u5339\u914d\u8fd9\u6bb5URL\u8def\u5f84\u3002<\/p>\n<p>\u8fd9\u91cc\u4e0d\u9700\u8981\u6dfb\u52a0URL\u6269\u5c55\u540d\uff0c\u5c31\u50cf.html\u2014\u2014\u9664\u975e\u4f60\u975e\u8981\u8fd9\u4e48\u505a\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f60\u80fd\u8fd9\u6837\u505a\uff1a<\/p>\n<pre>path('polls\/latest.html', views.index),\r\n<\/pre>\n<p>\u4f46\u662f\u8fd9\u4e48\u505a\u5f88\u50bb\u3002<\/p>\n<h1>\u5199\u4e00\u4e2a\u6709\u5b9e\u9645\u4f5c\u7528\u7684\u89c6\u56fe<\/h1>\n<p>\u6bcf\u4e00\u4e2a\u89c6\u56fe\u53ef\u4ee5\u505a\u4e24\u79cd\u4e8b\u60c5\u4e2d\u7684\u4e00\u79cd\uff1a(1)\u8fd4\u56de\u4e00\u4e2a<code>HttpResponse<\/code>\u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u8bf7\u6c42\u9875\u7684\u5185\u5bb9\u3002(2)\u5f15\u53d1\u4e00\u4e2a\u5f02\u5e38\uff0c\u5c31\u50cf<code>Http404<\/code>\uff0c\u5269\u4e0b\u7684\u53d6\u51b3\u4e8e\u4f60\u600e\u4e48\u505a\u3002<\/p>\n<p>\u4f60\u7684\u89c6\u56fe\u80fd\u4ece\u6570\u636e\u5e93\u8bfb\u8bb0\u5f55\uff0c\u80fd\u4f7f\u7528\u6a21\u677f\u7cfb\u7edf\uff0c\u80fd\u4ea7\u751fPDF\u6587\u4ef6\uff0c\u80fd\u8f93\u51faXML\uff0c\u80fd\u521b\u5efaZIP\u6587\u4ef6\uff0c\u4efb\u4f55\u4f60\u60f3\u505a\u7684\u4e8b\uff0c\u4f7f\u7528\u4f60\u60f3\u7528\u7684PYTHON\u5e93\u3002<\/p>\n<p>\u5728\u7cfb\u7edf\u4e2d\u663e\u793a\u6700\u8fd15\u4e2apoll\u8bf7\u6c42\uff0c\u6839\u636e\u53d1\u5e03\u65e5\u671f\u7528\u9017\u53f7\u5206\u5f00\uff1a<\/p>\n<pre>polls\/views.py\r\n\r\nfrom django.http import HttpResponse\r\n\r\nfrom .models import Question\r\n\r\n\r\ndef index(request):\r\n    latest_question_list = Question.objects.order_by('-pub_date')[:5]\r\n    output = ', '.join([q.question_text for q in latest_question_list])\r\n    return HttpResponse(output)\r\n<\/pre>\n<p>\u8fd9\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u601d\u8003\u4e00\u4e0b\uff1a\u9875\u9762\u7684\u8bbe\u8ba1\u5728\u89c6\u56fe\u4e2d\u662f\u786c\u7f16\u7801\u7684\u3002\u5982\u679c\u4f60\u60f3\u6539\u53d8\u89c6\u56fe\u7684\u5916\u89c2\uff0c\u5c31\u5fc5\u987b\u7f16\u8f91PYTHON\u4ee3\u7801\u3002\u6240\u4ee5\uff0c\u8ba9\u6211\u4eec\u4f7f\u7528Django\u7684\u6a21\u677f\u7cfb\u7edf\uff0c\u4e3a\u89c6\u56fe\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\uff0c\u628a\u8bbe\u8ba1\u4ecePYTHON\u5206\u79bb\u51fa\u6765\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728<code>polls<\/code>\u76ee\u5f55\u91cc\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>templates<\/code>\u76ee\u5f55\u3002Django\u5c06\u4f1a\u5728\u8fd9\u91cc\u67e5\u627e\u6a21\u677f\u3002<\/p>\n<p>\u4f60\u9879\u76ee\u7684<code>TEMPLATES<\/code>\u63cf\u8ff0\u4e86DJANGO\u5c06\u5982\u4f55\u52a0\u8f7d\u548c\u63d0\u4f9b\u6a21\u677f\u3002\u9ed8\u8ba4\u7684\u8bbe\u7f6e\u914d\u7f6e\u4e86\u4e00\u4e2a<code>DjangoTemplates<\/code>\u540e\u53f0\uff0c\u5b83\u7684<code>APP_DIRS<\/code>\u9009\u9879\u8bbe\u7f6e\u6210True\u3002\u4e60\u60ef\u4e0a\uff0c<code>DjangoTemplates<\/code>\u5728\u6bcf\u4e00\u4e2a<code>INSTALLED_APPS<\/code>\u4e2d\u67e5\u627e\u201ctemplates\u201d\u5b50\u76ee\u5f55\u3002<\/p>\n<p>\u5728\u4f60\u521a\u5efa\u7acb\u7684<code>templates<\/code>\u76ee\u5f55\u91cc\uff0c\u521b\u5efa\u522b\u4e00\u4e2a\u540d\u4e3apolls\u7684\u76ee\u5f55\uff0c\u5e76\u4e14\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aindex.html\u7684\u6587\u4ef6\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f60\u7684\u6a21\u677f\u5e94\u8be5\u5728<code>polls\/templates\/polls\/index.html<\/code>\u3002\u56e0\u4e3a<code>app_directories<\/code>\u6a21\u677f\u7684\u52a0\u8f7d\u5de5\u4f5c\u548c\u4e0a\u9762\u63cf\u8ff0\u7684\u4e00\u6837\uff0c\u53ef\u4ee5\u7b80\u5355\u7684\u8fdb\u884c\u4e00\u4e2a\u8f6f\u8fde\u63a5\u5230polls\/index.html\u3002<br \/>\n<strong>\u6a21\u677f\u7684\u540d\u5b57\u7a7a\u95f4<\/strong><br \/>\n\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u653e\u4e0b\u6a21\u677f\uff0c\u76f4\u63a5\u79bb\u5f00polls\/templates\u76ee\u5f55\u4e86\uff0c\u4f46\u662f\u8fd9\u5b9e\u9645\u4e0a\u662f\u4e2a\u574f\u4e3b\u610f\u3002Django\u4f1a\u9009\u62e9\u7b2c\u4e00\u4e2a\u5b83\u627e\u5230\u540d\u5b57\u5339\u914d\u7684\u6a21\u677f\uff0c\u5e76\u4e14\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u6a21\u677f\u5728\u4e0d\u540c\u7684\u5e94\u7528\u4f7f\u7528\u540c\u6837\u7684\u540d\u5b57\uff0cDjango\u5c06\u4e0d\u80fd\u533a\u5206\u5b83\u4eec\u3002\u6211\u4eec\u9700\u8981\u4e3aDjango\u6307\u51fa\u6b63\u786e\u7684\u90a3\u4e00\u4e2a\uff0c\u5e76\u4e14\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u540d\u5b57\u7a7a\u95f4\u6765\u533a\u5206\u4ed6\u4eec\u3002\u56e0\u6b64\uff0c\u8981\u628a\u8fd9\u4e9b\u6a21\u677f\u653e\u5230\u81ea\u5df1\u5e94\u7528\u76ee\u5f55\u4e2d\u5e94\u7528\u540d\u7684\u76ee\u5f55\u4e2d\uff08That is, by putting those templates inside <em>another<\/em> directory named for the application itself.\uff09\u3002<\/p>\n<p>\u5982\u4e0b\u4ee3\u7801\u653e\u5165\u6a21\u677f\u4e2d\uff1a<\/p>\n<pre>polls\/templates\/polls\/index.html\r\n{% if latest_question_list %}\r\n    &lt;ul&gt;\r\n    {% for question in latest_question_list %}\r\n        &lt;li&gt;&lt;a href=\"\/polls\/{{ question.id }}\/\"&gt;{{ question.question_text }}&lt;\/a&gt;&lt;\/li&gt;\r\n    {% endfor %}\r\n    &lt;\/ul&gt;\r\n{% else %}\r\n    &lt;p&gt;No polls are available.&lt;\/p&gt;\r\n{% endif %}\r\n<\/pre>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u5728polls\/views.py\u4e2d\u66f4\u65b0\u4e00\u4e0bindex\u89c6\u56fe\uff0c\u8ba9\u5176\u4f7f\u7528\u6a21\u677f\uff1a<\/p>\n<pre>polls\/views.py\r\n\r\nfrom django.http import HttpResponse\r\nfrom django.template import loader\r\n\r\nfrom .models import Question\r\n\r\n\r\ndef index(request):\r\n    latest_question_list = Question.objects.order_by('-pub_date')[:5]\r\n    template = loader.get_template('polls\/index.html')\r\n    context = {'latest_question_list': latest_question_list, }\r\n    return HttpResponse(template.render(context, request))\r\n<\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u52a0\u8f7d\u4e86\u540d\u4e3a<code>polls\/index.html<\/code>\u7684\u6a21\u677f\uff0c\u5e76\u4e14\u53d1\u9001\u4e86\u5b83\u7684\u201c\u5185\u5bb9\u201d\u3002\u8fd9\u4e2a\u201c\u5185\u5bb9\u201d\u662f\u6620\u5c04\u4e86\u6a21\u677f\u53d8\u91cf\u540d\u5230PYTHON\u5bf9\u8c61\u7684\u76ee\u5f55\u3002<\/p>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165\u201c\/polls\/\u201d\u52a0\u8f7d\u9875\u9762\uff0c\u4f1a\u770b\u5230\u4e00\u4e2a\u9879\u76ee\u7b26\u53f7\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6211\u4eec\u7684\u201c\u95ee\u9898\u201d\u3002\u8fd9\u4e9b\u95ee\u9898\u8fde\u63a5\u5230\u95ee\u9898 \u7684\u8be6\u60c5(detail\u89c6\u56fe)\u9875\u9762\u3002<\/p>\n<h1>\u5feb\u6377\u65b9\u5f0f\uff1arender()<\/h1>\n<p>\u8fd9\u662f\u7528\u6765\u52a0\u8f7d\u6a21\u677f\u7684\u5e38\u7528\u7528\u6cd5\uff0c\u586b\u5165\u5185\u5bb9\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e26\u6709\u6a21\u677f\u7ed3\u679c\u7684HttpResponse\u5bf9\u8c61\u3002Django\u63d0\u4f9b\u4e00\u4e2a\u5feb\u6377\u65b9\u5f0f\u3002\u91cd\u5199index()\u89c6\u56fe\uff1a<\/p>\n<pre>polls\/views.py\r\n\r\nfrom django.shortcuts import render\r\n\r\nfrom .models import Question\r\n\r\n\r\ndef index(request):\r\n    latest_question_list = Question.objects.order_by('-pub_date')[:5]\r\n    context = {'latest_question_list': latest_question_list}\r\n    return render(request, 'polls\/index.html', context)\r\n<\/pre>\n<p>\u6ce8\u610f\uff1a\u4e00\u65e6\u6211\u4eec\u5728\u6240\u6709\u89c6\u56fe\u4e2d\u4f7f\u7528\u4e86\u8fd9\u79cd\u5feb\u6377\u65b9\u5f0f\uff0c\u6211\u4eec\u5c31\u4e0d\u7528\u518d\u5bfc\u5165<code>loader<\/code>\u548c<code>HttpResponse<\/code>(\u5982\u679c\u4f60\u4ecd\u7136\u6709\u539f\u59cb\u7684\u65b9\u6cd5\uff0c\u5982\uff1adetail,results,vote\u3002\u90a3\u4e48\u4f60\u8fd8\u662f\u9700\u8981\u5bfc\u5165<code>HttpResponse<\/code>\u7684)\u3002<\/p>\n<p>render()\u51fd\u6570\u7528request\u5bf9\u8c61\u505a\u4e3a\u5b83\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u6a21\u677f\u540d\u505a\u4e3a\u5b83\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u9009\u586b\u7684\u5b57\u5178(dict)\u7c7b\u578b\uff08\u9700\u8981\u6dfb\u52a0\u5230\u6a21\u677f\u4e2d\u7684\u5185\u5bb9context\uff09\u3002\u5b83\u4f1a\u6839\u636e\u6240\u9009\u62e9\u7684\u6a21\u677f\u586b\u5199\u4e0a\u7ed9\u4e88\u7684\u5185\u5bb9(context)\u540e\u8fd4\u56de\u4e00\u4e2a<code>HttpResponse<\/code>\u5bf9\u8c61\u3002<\/p>\n<h1>\u53d1\u51fa404\u9519\u8bef<\/h1>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u6765\u5904\u7406\u4e00\u4e0bdetail\u89c6\u56fe\uff1a<\/p>\n<pre>polls\/views.py\r\n\r\nfrom django.http import Http404\r\nfrom django.shortcuts import render\r\n\r\nfrom .models import Question\r\n# ...\r\ndef detail(request, question_id):\r\n    try:\r\n        question = Question.objects.get(pk=question_id)\r\n    except Question.DoesNotExist:\r\n        raise Http404(\"Question does not exist\")\r\n    return render(request, 'polls\/detail.html', {'question': question})\r\n<\/pre>\n<p>\u4e00\u4e2a\u65b0\u7684\u6982\u5ff5:\u5982\u679c\u8bf7\u6c42\u7684\u9875\u9762\u4e0d\u5b58\u5728\uff0c\u89c6\u56fe\u53d1\u51fa\u4e00\u4e2a<code>Http404<\/code>\u5f02\u5e38\u3002<br \/>\n\u73b0\u5728\u60f3\u8ba9\u4e0a\u9762\u7684\u4f8b\u5b50\u8fd0\u884c\uff0c\u9700\u8981\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u677f\u6765\u652f\u6491\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre>polls\/templates\/polls\/detail.html\r\n\r\n{{ question }}\r\n<\/pre>\n<p>\u73b0\u5728\u542f\u52a8\u670d\u52a1\u5668\u5c31\u80fd\u770b\u5230\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<h1>\u5feb\u6377\u65b9\u5f0f\uff1aget_object_or_404()<\/h1>\n<p>\u5982\u679c\u5bf9\u8c61\u4e0d\u5b58\u5728\uff0c\u5e38\u4f7f\u7528get()\u548cHttp404()\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002Django\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5feb\u6377\u65b9\u5f0f\u3002\u91cd\u5199detail()\u89c6\u56fe\u6765\u770b\u4e00\u4e0b\uff1a<\/p>\n<pre>polls\/views.py\r\n\r\nfrom django.shortcuts import get_object_or_404, render\r\nfrom .models import Question\r\n# ...\r\ndef detail(request, question_id):\r\n    question = get_object_or_404(Question, pk=question_id)\r\n    return render(request, 'polls\/detail.html', {'question': question})\r\n<\/pre>\n<p><code>get_object_or_404()<\/code>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2aDjango\u6a21\u578b\uff0c\u540e\u9762\u662f\u4efb\u610f\u4e2a\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5b83\u628a\u8fd9\u4e9b\u53c2\u6570\u4f20\u9012\u7ed9<code>get()<\/code>\u51fd\u6570\uff0c\u5982\u679c\u5bf9\u8c61\u4e0d\u5b58\u5728\u4f1a\u5f15\u53d1<code>Http404<\/code>\u5f02\u5e38\u3002<\/p>\n<p>\u540c\u6837\u8fd8\u6709\u4e00\u4e2a<code>get_list_or_404()<\/code>\u51fd\u6570\uff0c\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u548c<code>get_object_or_404()<\/code>\u4e00\u6837\u2014\u2014\u9664\u4e86\u4f7f\u7528<code>filter()<\/code>\u4ee3\u66ff<code>get()<\/code>\u3002\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u4f1a\u5f15\u53d1<code>Http404<\/code>\u3002<\/p>\n<h1>\u4f7f\u7528\u6a21\u677f\u7cfb\u7edf<\/h1>\n<p>\u7f16\u8f91\u4e00\u4e0bpolls\/detail.html\u6a21\u677f\uff1a<\/p>\n<pre>&lt;h1&gt;{{question.question_text}}&lt;\/h1&gt;\r\n&lt;ul&gt;\r\n{% for choice in question.choice_set.all %}\r\n    &lt;li&gt;{{ choice.choice_text }}&lt;\/li&gt;\r\n&lt;% endfor %}\r\n&lt;\/ul&gt;<\/pre>\n<p>\u6a21\u677f\u7cfb\u7edf\u4f7f\u7528\u201c\u70b9\u67e5\u627e\u201d\u8bed\u6cd5\u8bbf\u95ee\u53d8\u91cf\u5c5e\u6027\u3002\u5728\u672c\u4f8b\u4e2d\uff0c{{ question.question_text }}\uff0c\u9996\u5148Django\u5148\u5bf9<strong>question<\/strong>\u5bf9\u8c61\u8fdb\u884c\u5b57\u5178\u67e5\u627e\u3002\u5982\u679c\u4e0d\u884c\uff0c\u5b83\u5c1d\u8bd5\u8fdb\u884c\u5c5e\u6027\u67e5\u627e\u2014\u2014\u5728\u672c\u4f8b\u4e2d\u5c5e\u6027\u67e5\u627e\u6210\u529f\u4e86\u3002\u5982\u679c\u5c5e\u6027\u67e5\u627e\u5931\u8d25\u4e86\uff0c\u5b83\u5c06\u4f1a\u5c1d\u8bd5\u5217\u8868\u7d22\u5f15\u67e5\u627e\u3002<\/p>\n<p>\u65b9\u6cd5\u8c03\u7528\u53d1\u751f\u5728<strong>{% for %}<\/strong>\u5faa\u73af\u4e2d\uff1a<strong>question.choice_set.all<\/strong>\u7684\u529f\u80fd\u662f\u8fd4\u56de<strong>Choice<\/strong>\u5bf9\u8c61\u7684\u8fed\u4ee3\u5668\uff0c\u4e4b\u540e\u7528\u4e8e<strong>{% for %}<\/strong>\u6bb5\u3002<\/p>\n<h1>\u5728\u6a21\u677f\u4e2d\u79fb\u9664\u786c\u94fe\u63a5<\/h1>\n<p>\u8bb0\u6ce8\uff1a\u5f53\u6211\u4eec\u5728polls\/index.html\u6a21\u677f\u4e2d\u7ed9question\u4e00\u4e2a\u94fe\u63a5\u65f6\uff0c\u786c\u94fe\u63a5\u7684\u90e8\u5206\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;li&gt;&lt;a href=\"\/polls\/{{ question.id }}\/\"&gt; {{ question.question_text }} &lt;\/a&gt;&lt;\/li&gt;\r\n<\/pre>\n<p>\u8fd9\u4e2a\u786c\u94fe\u63a5\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u5728\u9879\u76ee\u4e2d\u5927\u91cf\u7684\u6a21\u677f\u91cc\u4f7f\u7528\u8fd9\u79cd\u7d27\u5bc6\u8026\u5408\u7684\u65b9\u6cd5\u4e0d\u5229\u4e8e\u6539\u53d8URL\u3002\u4e0d\u8fc7\uff0c\u5728polls.urls\u6a21\u5757\u4e2dpath()\u51fd\u6570\u7684\u540d\u5b57\u53c2\u6570\uff08\u6700\u540e\u4e00\u4e2a\u53c2\u6570\uff09\u53ef\u4ee5\u79fb\u9664\u7279\u5b9aURL\u7684\u4f9d\u8d56\u6027\uff0c\u901a\u8fc7\u5728\u6a21\u677f\u4e2d\u4f7f\u7528\u6a21\u677f\u6807\u8bb0{% url %}\u8c03\u7528URL\u540d\uff1a<\/p>\n<pre>&lt;li&gt;&lt;a href=\"{% url <span style=\"color: #ff0000;\">'detail'<\/span> question.id %}\"&gt;{{ question.question_text }}&lt;\/a&gt;&lt;\/li&gt;\r\n<\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u901a\u8fc7\u67e5\u627e\u5728polls.urls\u6a21\u5757\u5185\u5b9a\u4e49\u7684URL\u540d\u3002\u540d\u4e3a\u201cdetail\u201d\u7684URL\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre>...\r\n# 'name'\u7684\u503c\u88ab{% url %}\u6a21\u5757\u6807\u7b7e\u8c03\u7528\r\npath('&lt;int:question_id\/', views.detail, name='detail'),\r\n...\r\n<\/pre>\n<p>\u5982\u679c\u4f60\u60f3\u6539\u53d8polls\u7684detail\u89c6\u56fe\u7684URL\uff08\u6bd4\u5982\u6539\u6210\uff1apolls\/specifics\/12\/\uff09\uff0c\u53ef\u4ee5\u5728polls\/urls.py\u4e2d\u8fd9\u6837\u4fee\u6539\uff1a<\/p>\n<pre>#\u6dfb\u52a0\u5173\u952e\u5b57'specifics'\r\npath('specifics\/&lt;int:question_id&gt;\/', views.detail, name='detail'),\r\n...\r\n<\/pre>\n<h1>URL\u540d\u5b57\u7a7a\u95f4<\/h1>\n<p>\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\u53ea\u6709\u4e00\u4e2a\u5e94\u7528\u2014\u2014polls\uff0c\u4f46\u662f\u5728\u771f\u5b9e\u7684\u9879\u76ee\u4e2d\u53ef\u80fd\u6709\u5f88\u591a\u5e94\u7528\uff0c\u8fd9\u65f6\u4e0d\u540c\u7684\u5e94\u7528\u4e2d\u90fd\u6709\u547d\u540d\u4e3adetail\u7684URL\u600e\u4e48\u529e\uff1f\u65b9\u6cd5\u662f\u5728URLconf\u4e2d\u6dfb\u52a0\u540d\u5b57\u7a7a\u95f4\u3002<\/p>\n<p>\u6253\u5f00polls\/urls.py\u6587\u4ef6\uff0c\u5728\u5f00\u5934\u6dfb\u52a0<code>app_name<\/code>\u7528\u4e8e\u8bbe\u7f6e\u540d\u5b57\u7a7a\u95f4\uff1a<\/p>\n<pre>from django.urls import path\r\nfrom . import views\r\napp_name = 'polls'\r\nurlpatterns=[path('',views.index, name='index'),\r\n             ...]\r\n<\/pre>\n<p>\u73b0\u5728\u6539\u53d8\u6a21\u677fpolls\/index.html\u4e2d\u7684\u8c03\u7528\u540d\uff1a<\/p>\n<pre>&lt;li&gt;&lt; href=\"{% url '<span style=\"color: #ff0000;\">polls<\/span>:detail' question.id %}\"&gt; {{ question.question_text}}&lt;\/a&gt;&lt;\/li&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728DJANGO\u4e2d\uff0cWEB\u9875\u9762\u548c\u5176\u5b83\u5185\u5bb9\u90fd\u7531\u89c6\u56fe\u4ea7\u751f\u3002\u6bcf\u4e00\u4e2a\u89c6\u56fe\u90fd\u662f\u7531\u4e00\u4e2a\u7b80\u5355\u7684PYTHON\u51fd\u6570\uff08\u6216\u65b9\u6cd5\uff0c\u5728\u57fa\u4e8e\u7c7b\u7684\u89c6\u56fe\u4e2d\uff09\u63cf\u8ff0\u7684\u3002DJANGO\u5c06\u901a\u8fc7\u6d4b\u8bd5\u8bf7\u6c42\uff08R&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=1176\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"class_list":["post-1176","post","type-post","status-publish","format-standard","hentry","category-django"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/1176","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=1176"}],"version-history":[{"count":16,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/1176\/revisions"}],"predecessor-version":[{"id":1192,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/1176\/revisions\/1192"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}