{"id":204027,"date":"2025-05-29T09:04:54","date_gmt":"2025-05-29T01:04:54","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204027\/"},"modified":"2025-05-29T09:04:54","modified_gmt":"2025-05-29T01:04:54","slug":"%e7%90%86%e8%a7%a3-django-%e7%9a%84%e6%9e%b6%e6%9e%84%ef%bc%9amtv-%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204027\/","title":{"rendered":"\u7406\u89e3 Django \u7684\u67b6\u6784\uff1aMTV \u6a21\u5f0f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7406\u89e3 Django \u7684\u67b6\u6784\uff1aMTV \u6a21\u5f0f<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u7406\u89e3 Django \u7684\u67b6\u6784\uff1aMTV \u6a21\u5f0f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241020\/17294206036714dd3b15f39.jpg\" class=\"aligncenter\"><\/p>\n<p>django \u9075\u5faa mtv\uff08\u6a21\u578b-\u6a21\u677f-\u89c6\u56fe\uff09\u6a21\u5f0f\u8fdb\u884c web \u5f00\u53d1\u3002\u4ee5\u4e0b\u662f\u6bcf\u4e2a\u7ec4\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\uff1a<\/p>\n<p>\u6a21\u578b\uff1a\u5b9a\u4e49\u60a8\u7684\u6570\u636e\u7ed3\u6784\u5e76\u5904\u7406\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\uff0c\u5141\u8bb8\u60a8\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\u800c\u65e0\u9700\u624b\u52a8\u7f16\u5199 sql \u67e5\u8be2\u3002<\/p>\n<p>\u6a21\u677f\uff1a\u8d1f\u8d23\u6e32\u67d3 html \u5e76\u5c06\u6570\u636e\u5448\u73b0\u7ed9\u7528\u6237\u3002\u60a8\u53ef\u4ee5\u7f16\u5199\u4e0e django \u6a21\u677f\u8bed\u8a00 (dtl) \u6df7\u5408\u7684 html \u6765\u663e\u793a\u52a8\u6001\u5185\u5bb9\u3002<\/p>\n<p>\u89c6\u56fe\uff1a\u5145\u5f53\u4e1a\u52a1\u903b\u8f91\u5c42\u3002\u5b83\u8fde\u63a5\u6a21\u578b\u548c\u6a21\u677f\uff0c\u5904\u7406\u7528\u6237\u8bf7\u6c42\uff0c\u4e0e\u6a21\u578b\u4ea4\u4e92\uff0c\u5e76\u8fd4\u56de\u54cd\u5e94\uff08\u901a\u5e38\u662f\u4ece\u6a21\u677f\u6e32\u67d3\u7684 html\uff09\u3002<\/p>\n<p>django \u7684\u8bf7\u6c42-\u54cd\u5e94\u5468\u671f\u5982\u4f55\u5de5\u4f5c\uff1a<\/p>\n<ul>\n<li>\u7528\u6237\u8bf7\u6c42\u7f51\u9875\uff08\u901a\u8fc7 url\uff09\u3002<\/li>\n<li>django \u5c06 url \u5339\u914d\u5230\u89c6\u56fe\u3002<\/li>\n<li>\u89c6\u56fe\u4ece\u6a21\u578b\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u6a21\u677f\u3002<\/li>\n<li>\u6a21\u677f\u5c06\u6570\u636e\u5448\u73b0\u4e3a html \u5e76\u5c06\u5176\u4f5c\u4e3a\u54cd\u5e94\u53d1\u9001\u56de\u7ed9\u7528\u6237\u3002<\/li>\n<\/ul>\n<p><strong>\u7b2c 1 \u6b65\uff1a\u5728 django \u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u5e94\u7528<\/strong>\u3002<br \/> \u8bbe\u7f6e django \u540e\uff08\u5982\u4e0a\u4e00\u7bc7\u6587\u7ae0\u6240\u8ff0\uff09\uff0c\u8ba9\u6211\u4eec\u5728\u60a8\u7684\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u8fd0\u884c\u8fd9\u4e9b\u547d\u4ee4\uff1a<\/p>\n<pre>cd mysite\npython3 manage.py startapp core\n<\/pre>\n<p>\u8fd9\u4f1a\u5728\u60a8\u7684 mysite \u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a core \u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u60a8\u7684\u6587\u4ef6\u7ed3\u6784\u73b0\u5728\u5e94\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>.\n\u251c\u2500\u2500 core\n\u2502   \u251c\u2500\u2500 admin.py\n\u2502   \u251c\u2500\u2500 apps.py\n\u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u251c\u2500\u2500 migrations\n\u2502   \u2502   \u2514\u2500\u2500 __init__.py\n\u2502   \u251c\u2500\u2500 models.py\n\u2502   \u251c\u2500\u2500 tests.py\n\u2502   \u2514\u2500\u2500 views.py\n\u251c\u2500\u2500 db.sqlite3\n\u251c\u2500\u2500 manage.py\n\u2514\u2500\u2500 mysite\n    \u251c\u2500\u2500 asgi.py\n    \u251c\u2500\u2500 __init__.py\n    \u251c\u2500\u2500 settings.py\n    \u251c\u2500\u2500 urls.py\n    \u2514\u2500\u2500 wsgi.py\n<\/pre>\n<p><strong>\u7b2c 2 \u6b65\uff1a\u5728\u8bbe\u7f6e\u6587\u4ef6\u4e2d\u6ce8\u518c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f<\/strong>\u3002<br \/> \u4e3a\u4e86\u8ba9 django \u77e5\u9053\u65b0\u5e94\u7528\u7a0b\u5e8f\uff0c\u60a8\u9700\u8981\u5c06\u5176\u6dfb\u52a0\u5230 mysite\/settings.py \u4e2d\u7684 installed_apps \u4e2d\uff1a<\/p>\n<pre>installed_apps = [\n    'django.contrib.admin',\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n    'django.contrib.messages',\n    'django.contrib.staticfiles',\n    'core',  # add this line\n]\n<\/pre>\n<p><strong>\u7b2c 3 \u6b65\uff1a\u521b\u5efa\u57fa\u672c\u89c6\u56fe<\/strong>\u3002<br \/> \u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u8fd4\u56de\u201chello, world!\u201d\u7684\u7b80\u5355\u89c6\u56fe\u3002\u7559\u8a00\u3002<\/p>\n<p>\u5728\u6838\u5fc3\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6253\u5f00views.py\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>from django.http import httpresponse\n\ndef learn(request):\n    return httpresponse(\"hello, world!\")\n<\/pre>\n<p><strong>\u7b2c 4 \u6b65\uff1a\u5c06 url \u6620\u5c04\u5230\u89c6\u56fe<\/strong>\u3002<br \/> \u8981\u901a\u8fc7 url \u8bbf\u95ee\u6b64\u89c6\u56fe\uff0c\u60a8\u9700\u8981\u5c06\u5176\u6620\u5c04\u5230 core\/urls.py \u6587\u4ef6\u4e2d\u3002\u5982\u679c\u8be5\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u8be5\u6587\u4ef6\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>from django.urls import path\nfrom . import views\n\nurlpatterns = [\n    path('learn\/', views.learn, name='learn'),\n]\n<\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u5c06\u6838\u5fc3\u5e94\u7528\u7a0b\u5e8f\u7684 url \u5305\u542b\u5728\u4e3b mysite\/urls.py \u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre>from django.contrib import admin\nfrom django.urls import include, path\n\nurlpatterns = [\n    path('admin\/', admin.site.urls),\n    path('core\/', include('core.urls')),  # include the core app urls\n]\n\n<\/pre>\n<p>\u73b0\u5728\uff0c\u5982\u679c\u60a8\u8fd0\u884c\u670d\u52a1\u5668\u5e76\u8bbf\u95ee http:\/\/127.0.0.1:8000\/core\/learn\/\uff0c\u60a8\u5e94\u8be5\u770b\u5230\u201chello, world!\u201d\u663e\u793a\u3002<br \/><strong>\u7b2c 5 \u6b65\uff1a\u521b\u5efa\u5e76\u6e32\u67d3\u6a21\u677f<\/strong><\/p>\n<pre>from django.shortcuts import render\n\ndef learn(request):\n    context = {'name': 'django'}\n    return render(request, 'hello.html', context)\n\n<\/pre>\n<p>\u6b64\u89c6\u56fe\u73b0\u5728\u5c06\u53d8\u91cf\uff08\u540d\u79f0\uff09\u4f20\u9012\u7ed9\u540d\u4e3a hello.html \u7684\u6a21\u677f\u3002<br \/><strong>\u7b2c 6 \u6b65\uff1a\u521b\u5efa\u6a21\u677f\u76ee\u5f55\u548c html \u6587\u4ef6<\/strong>.<br \/> \u5728\u60a8\u7684\u6838\u5fc3\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a templates \u6587\u4ef6\u5939\u548c\u4e00\u4e2a hello.html \u6587\u4ef6\uff1a<\/p>\n<pre>mkdir core\/templates\ntouch core\/templates\/hello.html\n<\/pre>\n<p>\u5728 hello.html \u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b html \u4ee3\u7801<\/p>\n<pre>&lt;!doctype html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;title&gt;hello template&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;hello, {{ name }}!&lt;\/h1&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p>\u5f53\u60a8\u518d\u6b21\u8bbf\u95ee\u5b66\u4e60 url \u65f6\uff0c\u60a8\u73b0\u5728\u5e94\u8be5\u770b\u5230\u201chello, django!\u201d\u5728\u6d4f\u89c8\u5668\u4e2d\u3002<\/p>\n<p><strong>\u7b2c 7 \u6b65\uff1a\u521b\u5efa\u57fa\u672c\u6a21\u578b<\/strong>.<br \/> \u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 post \u6a21\u578b\u6765\u5b58\u50a8\u535a\u5ba2\u6587\u7ae0\u3002<\/p>\n<p>\u5728 core\/models.py \u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>from django.db import models\n\nclass post(models.model):\n    title = models.charfield(max_length=100)\n    content = models.textfield()\n\n    def __str__(self):\n        return self.title\n<\/pre>\n<p>\u8fd9\u4e2a postmodel \u6709\u4e24\u4e2a\u5b57\u6bb5\uff1a\u6807\u9898\u548c\u5185\u5bb9\u3002 __str__ \u65b9\u6cd5\u786e\u4fdd post \u5bf9\u8c61\u5728 django \u7ba1\u7406\u6216 shell \u4e2d\u663e\u793a\u53ca\u5176\u6807\u9898<\/p>\n<p><strong>\u7b2c 8 \u6b65\uff1a\u5c06\u6a21\u578b\u5e94\u7528\u5230\u6570\u636e\u5e93<\/strong>\u3002<br \/> \u8981\u5728\u6570\u636e\u5e93\u4e2d\u521b\u5efa\u76f8\u5e94\u7684\u8868\uff0c\u8bf7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>python3 manage.py makemigrations\npython3 manage.py migrate\n<\/pre>\n<p>django \u73b0\u5728\u5c06\u4e3a post \u6a21\u578b\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u8868\u3002<\/p>\n<p>\u901a\u8fc7\u6267\u884c\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u5df2\u6210\u529f\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6a21\u578b\u3001\u89c6\u56fe\u548c\u6a21\u677f\u7684\u57fa\u672c django \u5e94\u7528\u7a0b\u5e8f\u3002\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\u6765\u6269\u5c55\u6b64\u57fa\u7840\uff0c\u4f8b\u5982\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u6539\u8fdb\u8bbe\u8ba1\u4ee5\u53ca\u4f7f\u5e94\u7528\u7a0b\u5e8f\u66f4\u5177\u4ea4\u4e92\u6027\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7406\u89e3 Django \u7684\u67b6\u6784\uff1aMT&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204027","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204027"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204027\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}