﻿{"id":240,"date":"2025-12-24T10:14:54","date_gmt":"2025-12-24T02:14:54","guid":{"rendered":"http:\/\/blog.huihuia24.top\/?p=240"},"modified":"2025-12-24T19:43:32","modified_gmt":"2025-12-24T11:43:32","slug":"django%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"http:\/\/blog.huihuia24.top\/index.php\/2025\/12\/24\/django%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0\/","title":{"rendered":"Django\u5b66\u4e60\u7b14\u8bb0"},"content":{"rendered":"<h2>\u4e00\u3001Django5\u7684\u4ecb\u7ecd<\/h2>\n<p>\u914d\u5957\u89c6\u9891\uff1a<a href=\"https:\/\/www.bilibili.com\/video\/BV1N1421U76L\/?spm_id_from=333.337.search-card.all.click&amp;vd_source=7d287d43097dc937ee4c68e2a2a4717d\">\u70b9\u51fb\u6b64\u5904<\/a><\/p>\n<h3>1. Django\u6846\u67b6\u7684\u4ecb\u7ecd<\/h3>\n<p>Django\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Web\u5e94\u7528\u6846\u67b6\uff0c\u4f7f\u7528python\u8bed\u8a00\u7f16\u5199\uff0c\u8bbe\u8ba1\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u5b89\u5168\u3001\u53ef\u6269\u5c55\u7684Web\u5e94\u7528\u7a0b\u5e8f<\/p>\n<h3>2.Django\u6846\u67b6\u7279\u70b9<\/h3>\n<p>MTV\u6a21\u5f0f\uff1a\u6a21\u578b(Model)\u3001\u6a21\u677f(Template)\u3001\u89c6\u56fe(View)<\/p>\n<p>\u81ea\u52a8\u5316\u7ba1\u7406\u540e\u53f0\uff1a\u63d0\u4f9b\u81ea\u52a8\u751f\u6210\u7ba1\u7406\u540e\u53f0\u7684\u529f\u80fd<\/p>\n<p>\u5f3a\u5927\u7684ORM\uff1a\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff0c\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c<\/p>\n<p>\u5b89\u5168\u6027\uff1a\u5185\u7f6e\u9632\u6b62\u5e38\u89c1\u653b\u51fb\uff0c\u5982\uff1aSQL\u6ce8\u5165\u3001\u8de8\u7ad9\u70b9\u8bf7\u6c42\u4f2a\u9020(CSRF)\u7684\u673a\u5236<\/p>\n<h3>3.Django\u7684\u5b89\u88c5<\/h3>\n<p>\u5728python\u7ec8\u7aef\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4<\/p>\n<p>\u5b89\u88c5\u865a\u62df\u73af\u5883<\/p>\n<pre><code class=\"language-sh\">python -m venv testdjango\n<\/code><\/pre>\n<p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/p>\n<pre><code class=\"language-sh\">testdjangoScriptsactivate\n<\/code><\/pre>\n<h3>4.\u5b89\u88c5Django<\/h3>\n<p>\u5728python\u7ec8\u7aef\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4<\/p>\n<pre><code class=\"language-sh\">pip install django\n<\/code><\/pre>\n<h2>\u4e8c\u3001Django\u5165\u95e8<\/h2>\n<h3>1.URL\u7ec4\u6210\u90e8\u5206<\/h3>\n<p>URL \u662f Uniform Resource Locator \u7684\u7b80\u5199\uff0c\u7edf\u4e00\u8d44\u6e90\u5b9a\u4f4d\u7b26<\/p>\n<p>\u4e00\u4e2aURL\u7531\u4e00\u4e0b\u51e0\u4e2a\u90e8\u5206\u7ec4\u6210<\/p>\n<pre><code class=\"language-http\">scheme:\/\/host:port\/path\/?query-string=xxx#anchor\n<\/code><\/pre>\n<ul>\n<li><strong>scheme<\/strong>\uff1a\u4ee3\u8868\u7684\u662f\u8bbf\u95ee\u7684\u534f\u8bae\uff0c\u4e00\u822c\u4e3a\uff1aHTTP\u3001HTTPS \u4ee5\u53ca FTP \u7b49<\/li>\n<li><strong>host<\/strong>\uff1a\u4e3b\u673a\u540d\u3001\u57df\u540d\uff0c\u6bd4\u5982<code>www.baidu.com<\/code><\/li>\n<li><strong>port<\/strong>\uff1a\u7aef\u53e3\u53f7\uff0cHTTP\u534f\u8bae\u662f80\u7aef\u53e3\uff0cHTTPS\u662f443\u7aef\u53e3<\/li>\n<li><strong>path<\/strong>\uff1a\u67e5\u627e\u8def\u5f84\uff0c\u6bd4\u5982\uff1a<code>www.jianshu.com\/trending\/now<\/code>\u540e\u9762\u7684 <code>trending\/now<\/code>\u5c31\u662f path<\/li>\n<li><strong>query-string<\/strong>\uff1a\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u6bd4\u5982\uff1a<code>www.baidu.com\/s?wd=python<\/code>\u540e\u9762\u7684<code>wd=python<\/code>\u5c31\u662f\u67e5\u8be2\u5b57\u7b26\u4e32<\/li>\n<li><strong>anchor<\/strong>\uff1a\u951a\u70b9\uff0c\u540e\u53f0\u4e00\u822c\u4e0d\u7528\u7ba1\uff0c\u524d\u7aef\u7528\u6765\u505a\u9875\u9762\u5b9a\u4f4d\u7684<\/li>\n<\/ul>\n<p><font color='red'><strong>\u6ce8\uff1a<\/strong><\/font>URL \u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u662f ASCII \u5b57\u7b26\u96c6\uff0c\u5982\u679c\u51fa\u73b0\u975e ASCII \u5b57\u7b26\uff0c\u6bd4\u5982\u4e2d\u6587\uff0c\u6d4f\u89c8\u5668\u4f1a\u8fdb\u7f16\u7801\u518d\u8fdb\u884c\u4f20\u8f93<\/p>\n<h3>2.\u521b\u5efaDjango\u9879\u76ee<\/h3>\n<h4>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528\u547d\u4ee4\u884c<\/h4>\n<ol>\n<li>\n<p>\u521b\u5efa\u9879\u76ee\uff1a\u6253\u5f00\u7ec8\u7aef\uff0c\u4f7f\u7528\u547d\u4ee4\uff1a<\/p>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"language-sh\">django-admin startproject \u9879\u76ee\u540d\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u521b\u5efa\u5e94\u7528(app)\uff1a\u4e00\u4e2a\u9879\u76ee\u7c7b\u4f3c\u4e0e\u662f\u4e00\u4e2a\u67b6\u5b50\uff0c\u4f46\u662f\u771f\u6b63\u8d77\u4f5c\u7528\u7684\u8fd8\u662fapp\uff0c\u5728\u7ec8\u7aef\u8fdb\u5165\u5230\u9879\u76ee\u6240\u5728\u8def\u5f84\uff0c\u7136\u540e\u6267\u884c<code>python manage.py startapp app\u540d\u79f0<\/code>\u521b\u5efa\u4e00\u4e2aapp<\/p>\n<\/li>\n<\/ol>\n<h4>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528Pycharm<\/h4>\n<p>\u7528 Pycharm \u65b0\u5efa\u4e00\u4e2a Django \u9879\u76ee<\/p>\n<p>\u6587\u4ef6 ==&gt; \u65b0\u5efa\u9879\u76ee ==&gt; Django ==&gt; \u9009\u62e9\u4f4d\u7f6e ==&gt; \u9009\u62e9\u89e3\u91ca\u5668 ==&gt; \u521b\u5efa<\/p>\n<p>\u4f7f\u7528Pycharm\u521b\u5efa\u5b8c\u9879\u76ee\u540e\uff0c\u8fd8\u662f\u9700\u8981\u91cd\u65b0\u8fdb\u5165\u547d\u4ee4\u884c\u5355\u72ec\u521b\u5efaapp\u7684<\/p>\n<h3>3.\u8fd0\u884cDjango\u9879\u76ee<\/h3>\n<h4>\u901a\u8fc7\u547d\u4ee4\u884c\u7684\u65b9\u5f0f<\/h4>\n<pre><code class=\"language-sh\">python manage.py runserver\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5728\u672c\u5730\u8bbf\u95ee\u4f60\u7684\u7f51\u7ad9\uff0c\u7aef\u53e3\u53f7\u9ed8\u8ba4\u662f 8000 \uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u901a\u8fc7<code>http:\/\/127.0.0.1:8000\/<\/code>\u6216\u8005<code>http:\/\/localhost:8000\/<\/code>\u6765\u8bbf\u95ee\u4f60\u7684\u7f51\u7ad9\u4e86\uff0c\u53e6\u5916\uff0c\u8fd9\u6837\u8fd0\u884c\u53ea\u80fd\u5728\u672c\u5730\u8bbf\u95ee\uff0c\u5982\u679c\u60f3\u8981\u5728\u5176\u4ed6\u7535\u8111\u4e0a\u4e5f\u80fd\u8bbf\u95ee\u672c\u7f51\u7ad9\uff0c\u90a3\u4e48\u9700\u8981\u6307\u5b9aip\u5730\u5740\u4e3a<code>0.0.0.0<\/code>\uff0c\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-sh\">python manage.py runserver 0.0.0.0:8000\n<\/code><\/pre>\n<h4>\u901a\u8fc7Pycharm\u8fd0\u884c<\/h4>\n<p>\u76f4\u63a5\u70b9\u8fd0\u884c\u5373\u53ef<\/p>\n<h3>4.\u9879\u76ee\u7ed3\u6784\u4ecb\u7ecd<\/h3>\n<ol>\n<li><code>manage.py<\/code>\uff1a\u4ee5\u540e\u548c\u9879\u76ee\u4ea4\u4e92\u57fa\u672c\u4e0a\u90fd\u662f\u57fa\u4e8e\u8fd9\u4e2a\u6587\u4ef6\uff0c\u4e00\u822c\u90fd\u662f\u5728\u7ec8\u7aef\u8f93\u5165<code>python manage.py \u5b50\u547d\u4ee4<\/code>\uff0c\u53ef\u4ee5\u8f93\u5165<code>python manage.py help<\/code>\u770b\u4e0b\u80fd\u505a\u4ec0\u4e48\u4e8b\u60c5\uff0c\u9664\u975e\u4f60\u77e5\u9053\u4f60\u81ea\u5df1\u5728\u505a\u4ec0\u4e48\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u4e0d\u5e94\u8be5\u7f16\u8f91\u8fd9\u4e2a\u6587\u4ef6<\/li>\n<li><code>settings.py<\/code>\uff1a\u672c\u9879\u76ee\u7684\u8bbe\u7f6e\u9879\uff0c\u4ee5\u540e\u6240\u6709\u548c\u9879\u76ee\u76f8\u5173\u7684\u914d\u7f6e\u90fd\u653e\u5728\u8fd9\u91cc<\/li>\n<li><code>urls.py<\/code>\uff1a\u8fd9\u4e2a\u6587\u4ef6\u7528\u6765\u914d\u7f6eURL\u8def\u7531\u7684\uff0c\u6bd4\u5982<code>http:\/\/127.0.0.1\/news\/<\/code>\u662f\u8bbf\u95ee\u65b0\u95fb\u5217\u8868\u9875\uff0c\u8fd9\u4e9b\u4e1c\u897f\u5c31\u9700\u8981\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u5b8c\u6210<\/li>\n<li><code>wsgi.py<\/code>\uff1a\u9879\u76ee\u4e0e<code>WSGI<\/code>\u534f\u8bae\u517c\u5bb9\u7684web\u670d\u52a1\u5668\u5165\u53e3\uff0c\u90e8\u7f72\u7684\u65f6\u5019\u9700\u8981\u7528\u5230\uff0c\u4e00\u822c\u4e0d\u9700\u8981\u4fee\u6539<\/li>\n<\/ol>\n<h3>5.project\u548capp\u7684\u5173\u7cfb<\/h3>\n<p>app \u662f Django \u9879\u76ee\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u4e00\u4e2a app \u4ee3\u8868\u9879\u76ee\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u6240\u6709URL\u8bf7\u6c42\u7684\u54cd\u5e94\u90fd\u662f\u7531 app \u6765\u5904\u7406\uff0c\u6bd4\u5982\u8c46\u74e3\uff0c\u91cc\u9762\u6709\u56fe\u4e66\u3001\u7535\u5f71\u3001\u97f3\u4e50\u3001\u540c\u57ce\u7b49\u8bb8\u8bb8\u591a\u591a\u7684\u6a21\u5757\uff0c\u5982\u679c\u7ad9\u5728 Django \u7684\u89d2\u5ea6\u6765\u770b\u7684\u8bdd\uff0c\u56fe\u4e66\u3001\u7535\u5f71\u8fd9\u4e9b\u6a21\u5757\u5c31\u662f app \uff0c\u56fe\u4e66\u3001\u7535\u5f71\u8fd9\u4e9b app \u5171\u540c\u7ec4\u6210\u8c46\u74e3\u8fd9\u4e2a\u9879\u76ee\uff0c\u56e0\u6b64\u8fd9\u91cc\u8981\u6709\u4e00\u4e2a\u6982\u5ff5\uff0cDjango \u9879\u76ee\u7531\u8bb8\u591a app \u7ec4\u6210\uff0c\u4e00\u4e2a app \u53ef\u4ee5\u88ab\u7528\u5230\u5176\u4ed6\u9879\u76ee\uff0cDjango \u4e5f\u80fd\u62e5\u6709\u4e0d\u540c\u7684 app<\/p>\n<p><strong>\u521b\u5efa app \u547d\u4ee4\u8bed\u6cd5<\/strong><\/p>\n<pre><code class=\"language-sh\">python manage.py startapp app\u540d\u79f0\n<\/code><\/pre>\n<h3>6.URL \u4e0e\u89c6\u56fe\u51fd\u6570\u7684\u6620\u5c04<\/h3>\n<pre><code class=\"language-python\"># urls.py\n\n# https:\/\/www.baidu.com\/s?wd=python\n# url \u4e0e\u89c6\u56fe\u6620\u5c04\n# \/s(url)  --&gt; \u89c6\u56fe\u51fd\u6570\uff0c\u8fdb\u884c\u6620\u5c04\n# \u9700\u6c42\uff1ahttp:\/\/localhost:8000\/ --&gt; \u8fd4\u56de\u201c\u6b22\u8fce\u8fdb\u5165\u6d4b\u8bd5\u9875\uff01\u201d\nfrom django.shortcuts import HttpResponse\n#\u5bfc\u5165\n# \u5b9a\u4e49\u89c6\u56fe\u51fd\u6570\ndef index(request):\n    # \u5fc5\u987b\u8fd4\u56deHttpResponse\u5bf9\u8c61\n    return HttpResponse(&quot;\u6b22\u8fce\u8fdb\u5165\u6d4b\u8bd5\u9875\uff01&quot;)\n# \u5982\u679c\u9700\u8981\u8fdb\u884c\u6620\u5c04\uff0c\u5c31\u5f97\u5728urlpatterns\u5217\u8868\u4e2d\u6dfb\u52a0\u5b9a\u4e49\n\nurlpatterns = [\n    path(&#039;admin\/&#039;, admin.site.urls),\n    path(&#039;&#039;, index),\n]\n<\/code><\/pre>\n<p><strong>\u62d3\u5c55\uff1a<\/strong><\/p>\n<p>\u5728<code>settings.py<\/code>\u4e2d<code>DEBUG = True<\/code>\u4fee\u6539\u9879\u76ee\u4efb\u610f\u4ee3\u7801\u540e\uff0c\u6309<code>ctrl+s<\/code>\u540e\u81ea\u52a8\u66f4\u65b0\uff0c\u4e0d\u9700\u8981\u91cd\u65b0\u542f\u52a8<\/p>\n<h3>7.URL\u7684\u4e24\u79cd\u4f20\u53c2\u65b9\u5f0f<\/h3>\n<h4>\u65b9\u6cd5\u4e00\uff1a\u901a\u8fc7\u67e5\u8be2\u5b57\u7b26\u4e32(query string)<\/h4>\n<p>book\/views.py<\/p>\n<pre><code class=\"language-python\">from django.shortcuts import render,HttpResponse\n\n# \u5728URL\u4e2d\u643a\u5e26\u53c2\u6570\n# 1. \u67e5\u8be2\u5b57\u7b26\u4e32:http:\/\/127.0.0.1:8000\/book?id=2&amp;name=xx\ndef book_detail_query_string(request):\n    # request.GET = {&#039;id&#039;:3}\n    book_id = request.GET.get(&#039;id&#039;)\n    book_name = request.GET.get(&#039;name&#039;)\n    return HttpResponse(f&#039;\u4f60\u67e5\u8be2\u7684\u56fe\u4e66id\u662f:{book_id},\u540d\u5b57\u662f:{book_name}&#039;)\n<\/code><\/pre>\n<p>urls.py<\/p>\n<pre><code class=\"language-python\">from book import views\nurlpatterns = [\n    path(&#039;admin\/&#039;, admin.site.urls),\n    # \u6dfb\u52a0\n    path(&#039;book&#039;,views.book_detail_query_string)\n]\n<\/code><\/pre>\n<h4>\u65b9\u6cd5\u4e8c\uff1a\u5728path\u4e2d\u643a\u5e26<\/h4>\n<p>urls.py<\/p>\n<pre><code class=\"language-python\">from book import views\nurlpatterns = [\n    path(&#039;admin\/&#039;, admin.site.urls),\n    # http:\/\/127.0.0.1:8000\/book\/2\n    path(&#039;book\/&lt;book_id&gt;&#039;,views.book_detail_path)\n]\n<\/code><\/pre>\n<p>book\/views.py<\/p>\n<pre><code class=\"language-python\">from django.shortcuts import render,HttpResponse\n\n# \u5728path\u4e2d\u643a\u5e26\ndef book_detail_path(request,book_id):\n    return HttpResponse(f&#039;\u4f60\u67e5\u8be2\u7684\u56fe\u4e66id\u662f:{book_id}&#039;)\n<\/code><\/pre>\n<p><strong>\u5728<code>book_id <\/code>\u524d\u6307\u5b9a\u53c2\u6570\u7c7b\u578b\u6709\u4e24\u70b9\u597d\u5904\uff1a<\/strong><\/p>\n<ol>\n<li>\u4ee5\u540e\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u5982\u679c<code>book_id<\/code>\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u975e\u6574\u578b\uff0c\u90a3\u4e48\u5c31\u4f1a\u51fa\u73b0404\u9519\u8bef<\/li>\n<li>\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\uff0c\u5f97\u5230\u7684<code>book_id<\/code>\u5c31\u662f\u4e2a\u6574\u578b\uff0c\u5426\u5219\u9ed8\u8ba4\u662f str \u7c7b\u578b<\/li>\n<\/ol>\n<p>\u9664\u4e86<code>int<\/code>\u7c7b\u578b Django \u7684 path \u90e8\u5206\u8fd8\u652f\u6301<code>str<\/code>\u3001<code>uuid<\/code>\u3001<code>path<\/code>\u7c7b\u578b<\/p>\n<h3>8.path \u51fd\u6570<\/h3>\n<p>path \u51fd\u6570\u7684\u5b9a\u4e49\u4e3a\uff1a<code>path(route,view,name=None,kwargs=None)<\/code>\uff0c\u4ee5\u4e0b\u5bf9\u8fd9\u4e2a\u51e0\u4e2a\u53c2\u6570\u8fdb\u884c\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li><code>route<\/code>\u53c2\u6570\uff1aURL\u7684\u5339\u914d\u89c4\u5219\uff0c\u8fd9\u4e2a\u53c2\u6570\u4e2d\u53ef\u4ee5\u6307\u5b9aURL\u4e2d\u9700\u8981\u4f20\u9012\u7684\u53c2\u6570\uff0c\u6bd4\u5982\u5728\u8bbf\u95ee\u6587\u7ae0\u8be6\u60c5\u9875\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a id \uff0c\u4f20\u8f93\u53c2\u6570\u662f\u901a\u8fc7<code>&lt;&gt;<\/code>\u5c16\u62ec\u53f7\u6765\u6307\u5b9a\u7684\uff0c\u5e76\u4e14\u5728\u4f20\u9012\u53c2\u6570\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u6307\u5b9a\u8fd9\u4e2a\u53c2\u6570\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982\u6587\u7ae0\u7684 id \u90fd\u662f int \u7c7b\u578b\uff0c\u90a3\u4e48\u4e45\u53ef\u4ee5\u8fd9\u6837\u5199<code>&lt;int:id&gt;<\/code>\uff0c\u4ee5\u540e\u5339\u914d\u7684\u65f6\u5019\uff0c\u5c31\u53ea\u4f1a\u5339\u914d\u5230 id \u4e3a int \u7c7b\u578b\u7684 URL \uff0c\u800c\u4e0d\u4f1a\u5339\u914d\u5176\u4ed6\u7684URL\uff0c\u5e76\u4e14\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\u83b7\u53d6\u8fd9\u4e2a\u53c2\u6570\u7684\u65f6\u5019\uff0c\u5c31\u5df2\u7ecf\u88ab\u8f6c\u6362\u6210\u4e00\u4e2a int \u7c7b\u578b\u4e86\uff0c\u5176\u4e2d\u8fd8\u6709\u51e0\u79cd\u5e38\u7528\u7684\u7c7b\u578b\uff1a\n<ul>\n<li><code>str<\/code>\uff1a\u975e\u7a7a\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u9ed8\u8ba4\u7684\u8f6c\u6362\u5668\uff0c\u4f46\u4e0d\u80fd\u5305\u542b\u659c\u6760<\/li>\n<li><code>int<\/code>\uff1a\u5339\u914d\u4efb\u610f\u76840\u6216\u8005\u6b63\u6574\u6570\u7684\u6574\u578b\u3002\u5230\u89c6\u56fe\u51fd\u6570\u4e2d\u5c31\u662f\u4e00\u4e2a int \u7c7b\u578b<\/li>\n<li><code>slug<\/code>\uff1a\u7531\u82f1\u6587\u4e2d\u7684\u6a2a\u6760<code>-<\/code>\u3001\u6216\u8005\u4e0b\u5212\u7ebf<code>_<\/code>\uff1b\u8fde\u63a5\u82f1\u6587\u5b57\u7b26\u6216\u8005\u6570\u5b57\u800c\u6210\u7684\u5b57\u7b26\u4e32<\/li>\n<li><code>uuid<\/code>\uff1a\u5339\u914d<code>uuid<\/code>\u5b57\u7b26\u4e32<\/li>\n<li><code>path<\/code>\uff1a\u5339\u914d\u975e\u7a7a\u7684\u82f1\u6587\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5305\u542b\u659c\u6760<code>\/<\/code><\/li>\n<\/ul>\n<\/li>\n<li><code>view<\/code>\u53c2\u6570\uff1a\u53ef\u4ee5\u4e3a\u4e00\u4e2a\u89c6\u56fe\u51fd\u6570\uff0c\u6216\u8005\u662f<code>\u7c7b\u89c6\u56fe.as_view()<\/code>\u6216\u8005\u662f<code>django.urls.include()<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c<\/li>\n<li><code>name<\/code>\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u662f\u7ed9\u8fd9\u4e2aURL\u53d6\u540d\u7684\uff0c\u8fd9\u5728\u9879\u76ee\u6bd4\u8f83\u5927\uff0cURL\u6bd4\u8f83\u591a\u7684\u65f6\u5019\u7528\u5904\u5f88\u5927<\/li>\n<\/ol>\n<h3>9.URL\u4e2d\u5305\u542b\u53e6\u5916\u4e00\u4e2a urls \u6a21\u5757<\/h3>\n<p>\u5728\u9879\u76ee\u4e2d\uff0c\u4e0d\u80fd\u53ea\u6709\u4e00\u4e2aapp\uff0c\u5982\u679c\u628a\u6240\u6709\u7684app\u7684views\u4e2d\u7684\u89c6\u56fe\u90fd\u653e\u5728<code>urls.py<\/code>\u4e2d\u8fdb\u884c\u6620\u5c04\uff0c\u80af\u5b9a\u4f1a\u8ba9\u4ee3\u7801\u663e\u5f97\u975e\u5e38\u4e71\uff0c\u56e0\u6b64 Django \u53ef\u4ee5\u5728 app \u5185\u90e8\u5305\u542b\u81ea\u5df1\u7684URL\u5339\u914d\u89c4\u5219\uff0c\u800c\u5728\u9879\u76ee\u7684<code>url.py<\/code>\u4e2d\u518d\u7edf\u4e00\u5305\u542b\u8fd9\u4e2aapp \u7684 urls\uff0c\u4f7f\u7528\u8fd9\u4e2a\u6280\u672f\u9700\u8981\u501f\u52a9 include \u51fd\u6570<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<p>\u5728 move \u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2a<code>urls.py<\/code>\u6587\u4ef6<\/p>\n<pre><code class=\"language-python\">from django.urls import path\nfrom . import views\n\n# \u6307\u5b9a\u5e94\u7528\u540d\u79f0(\u5e94\u7528\u547d\u540d\u7a7a\u95f4)\napp_name = &#039;move&#039;\n\nurlpatterns = [\n    path(&#039;list&#039;, views.move_list, name=&#039;move_list&#039;),\n    path(&#039;detail\/&lt;int:move_id&gt;&#039;,views.move_detail, name=&#039;move_detail&#039;),\n]\n<\/code><\/pre>\n<p> \u5728\u9879\u76ee\u7684<code>url.py<\/code>\u4e2d\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from django.contrib import admin\n# \u5bfc\u5165include\nfrom django.urls import path, include\nfrom book import views\nurlpatterns = [\n    path(&#039;admin\/&#039;, admin.site.urls),\n    # http:\/\/127.0.0.1:8000\/book\/2\n    path(&#039;book\/&lt;int:book_id&gt;&#039;,views.book_detail_path),\n\n    # \u52a0\u5165move\u6587\u4ef6\u4e2d\u7684url\n    path(&#039;move\/&#039;, include(&#039;move.urls&#039;)),\n]\n<\/code><\/pre>\n<h3>10.URL\u53cd\u8f6c<\/h3>\n<ul>\n<li>\u4e4b\u524d\u6211\u4eec\u90fd\u662f\u901a\u8fc7URL\u8bbf\u95ee\u89c6\u56fe\u51fd\u6570\uff0c\u6709\u65f6\u5019\u6211\u4eec\u77e5\u9053\u8fd9\u4e2a\u89c6\u56fe\u51fd\u6570\uff0c\u4f46\u662f\u60f3\u53cd\u8f6c\u56de\u4ed6\u7684URL\uff0c\u8fd9\u65f6\u5019\u5c31\u53ef\u4ee5\u901a\u8fc7<code>reverse<\/code>\u6765\u5b9e\u73b0<\/li>\n<li>\u5982\u679c\u6709\u547d\u540d\u7a7a\u95f4\u6216\u8005\u6709\u5b9e\u4f8b\u547d\u540d\u7a7a\u95f4\uff0c\u90a3\u4e48\u5e94\u8be5\u5728\u53cd\u8f6c\u7684\u65f6\u5019\u52a0\u4e0a\u547d\u540d\u7a7a\u95f4<\/li>\n<li>\u5982\u679c\u8fd9\u4e2aURL\u4e2d\u9700\u8981\u4f20\u9012\u53c2\u6570\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7<code>kwargs<\/code>\u6765\u4f20\u9012\u53c2\u6570<\/li>\n<li>\u56e0\u4e3a Django \u4e2d\u7684<code>reverse<\/code>\u53cd\u8f6cURL\u7684\u65f6\u5019\u4e0d\u533a\u5206 GET \u8bf7\u6c42\u548c POST \u8bf7\u6c42\uff0c\u56e0\u6b64\u4e0d\u80fd\u5728\u53cd\u8f6c\u7684\u65f6\u5019\u6dfb\u52a0\u67e5\u8be2\u5b57\u7b26\u4e32\u7684\u53c2\u6570\uff0c\u5982\u679c\u60f3\u8981\u6dfb\u52a0\u67e5\u8be2\u5b57\u7b26\u4e32\u7684\u53c2\u6570\uff0c\u53ea\u80fd\u624b\u52a8\u6dfb\u52a0<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">from django.contrib import admin\n# \u5bfc\u5165reverse\u51fd\u6570\nfrom django.urls import path, include,reverse\nfrom django.shortcuts import HttpResponse\nfrom book import views\n\ndef index(request):\n    # \u65e0\u53c2\u6570\u53cd\u8f6c\n    print(reverse(&#039;index&#039;))\n    # \u5e26\u53c2\u6570\u53cd\u8f6c\n    print(reverse(&#039;book_int_id&#039;, kwargs={&#039;book_id&#039;:1}))\n    # \u5982\u679c\u662f\u8fd9\u79cd\uff1a\/book?id=1 \u53ea\u80fd\u8fdb\u884c\u624b\u52a8\u4f20\u53c2\n    # print(reverse(&#039;index&#039;) + &#039;?id=1&#039;)\n\n    # \u5df2\u6709\u547d\u540d\u7a7a\u95f4\u7684URL\u53cd\u8f6c\n    print(reverse(&#039;move:move_detail&#039;, kwargs={&#039;move_id&#039;:1}))\n    return HttpResponse(&quot;\u6b22\u8fce\u52a0\u5165\u725b\u9a6c\u5927\u519b&quot;)\n\n\nurlpatterns = [\n    path(&#039;admin\/&#039;, admin.site.urls),\n    path(&#039;&#039;, index, name=&#039;index&#039;),\n\n    path(&#039;book\/&lt;int:book_id&gt;&#039;, views.book_id, name=&#039;book_int_id&#039;),\n    path(&#039;move\/&#039;, include(&#039;move.urls&#039;)),\n]\n<\/code><\/pre>\n<p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">\/\n\/book\/1\n\/book?id=1\n\/move\/detail\/1\n<\/code><\/pre>\n<h2>\u4e09\u3001\u6a21\u677f<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001Django5\u7684\u4ecb\u7ecd \u914d\u5957\u89c6\u9891\uff1a\u70b9\u51fb\u6b64\u5904 1. Django\u6846\u67b6\u7684\u4ecb\u7ecd Django\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Web\u5e94\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":78,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[22,12],"class_list":["post-240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-django","tag-python"],"_links":{"self":[{"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/posts\/240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/comments?post=240"}],"version-history":[{"count":1,"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/posts\/240\/revisions"}],"predecessor-version":[{"id":241,"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/posts\/240\/revisions\/241"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/media\/78"}],"wp:attachment":[{"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/media?parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/categories?post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.huihuia24.top\/index.php\/wp-json\/wp\/v2\/tags?post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}