{"id":2066,"date":"2023-04-01T21:37:35","date_gmt":"2023-04-01T13:37:35","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2066"},"modified":"2023-04-06T16:02:20","modified_gmt":"2023-04-06T08:02:20","slug":"getting-started-with-python-web-framework-django-template","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/getting-started-with-python-web-framework-django-template\/","title":{"rendered":"Python Web\u6846\u67b6Django\u6a21\u677f\u5165\u95e8"},"content":{"rendered":"<p>\u521b\u5efa\u76ee\u5f55<code>HelloWorld\/templates<\/code>\u5e76\u5efa\u7acb<code>appblog.html<\/code>\u6587\u4ef6<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;Hello&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;{{ hello }}&lt;\/h1&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u4fee\u6539\u6587\u4ef6<code>HelloWorld\/HelloWorld\/settings.py<\/code>\uff0c\u4fee\u6539<code>TEMPLATES<\/code>\u4e2d\u7684<code>DIRS<\/code>\u4e3a<code>[BASE_DIR+&quot;\/templates&quot;,]<\/code><\/p>\n<pre><code class=\"language-python\">...\nTEMPLATES = [\n    {\n        &#039;BACKEND&#039;: &#039;django.template.backends.django.DjangoTemplates&#039;,\n        &#039;DIRS&#039;: [BASE_DIR+&quot;\/templates&quot;,],       # \u4fee\u6539\u4f4d\u7f6e\n        &#039;APP_DIRS&#039;: True,\n        &#039;OPTIONS&#039;: {\n            &#039;context_processors&#039;: [\n                &#039;django.template.context_processors.debug&#039;,\n                &#039;django.template.context_processors.request&#039;,\n                &#039;django.contrib.auth.context_processors.auth&#039;,\n                &#039;django.contrib.messages.context_processors.messages&#039;,\n            ],\n        },\n    },\n]\n...<\/code><\/pre>\n<p>\u4fee\u6539\u6587\u4ef6<code>HelloWorld\/HelloWorld\/views.py<\/code>\uff0c\u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u7528\u4e8e\u5411\u6a21\u677f\u63d0\u4ea4\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-python\">from django.shortcuts import render\n\ndef appblog(request):\n    context = {}\n    context[&#039;hello&#039;] = &#039;http:\/\/www.appblog.cn&#039;\n    return render(request, &#039;test.html&#039;, context)<\/code><\/pre>\n<p>\u914d\u7f6e\u8def\u7531<code>HelloWorld\/HelloWorld\/urls.py<\/code><\/p>\n<pre><code class=\"language-python\">from django.urls import path\nfrom . import views\n\nurlpatterns = [\n    path(&#039;appblog\/&#039;, views.appblog),\n]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u521b\u5efa\u76ee\u5f55HelloWorld\/templates\u5e76\u5efa\u7acbappblog.html\u6587\u4ef6 &lt;!DOCTYPE  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[522],"class_list":["post-2066","post","type-post","status-publish","format-standard","hentry","category-python","tag-django"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2066","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2066"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2066\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}