{"id":311,"date":"2021-11-09T19:40:39","date_gmt":"2021-11-09T11:40:39","guid":{"rendered":"http:\/\/47.98.165.52\/?p=311"},"modified":"2023-02-20T10:37:19","modified_gmt":"2023-02-20T02:37:19","slug":"django-3-templates-%e6%a8%a1%e6%9d%bf%e5%8f%98%e9%87%8f%e5%8f%8a%e6%a8%a1%e6%9d%bf%e8%bf%87%e6%bb%a4%e5%99%a8","status":"publish","type":"post","link":"\/?p=311","title":{"rendered":"Django-3.templates-\u6a21\u677f\u53d8\u91cf\u53ca\u6a21\u677f\u8fc7\u6ee4\u5668"},"content":{"rendered":"<h1>\u4e00\u3001\u6a21\u677f\u8def\u5f84\u603b\u7ed3<\/h1>\n<ul>\n<li>\u5728\u914d\u7f6e\u6587\u4ef6settings.py\u4e2d\u627e\u5230TEMPLATES\u8bbe\u7f6e\u6765\u914d\u7f6e<\/li>\n<li>\u8fd9\u662f\u4e00\u4e2a\u8bbe\u7f6e\u9009\u9879\u7684\u5217\u8868\uff0c\u6a21\u677f\u5927\u90fd\u5305\u542b\u4e24\u9879\u901a\u7528\u8bbe\u7f6e\uff1b\u4e24\u79cd\u65b9\u5f0f\u914d\u7f6e\u6a21\u677f\uff1a\n<ol>\n<li>\u7b2c\u4e00\u79cd\uff1aDIRS\u5b9a\u4e49\u4e00\u4e2a\u76ee\u5f55\u5217\u8868\uff0c\u6a21\u677f\u5f15\u64ce\u6309\u5217\u8868\u987a\u5e8f\u641c\u7d22\u8fd9\u4e9b\u76ee\u5f55\uff0c\u4ee5\u67e5\u627e\u6a21\u677f\u6e90\u6587\u4ef6\u3001\u5c06templates\u653e\u5728\u4e3b\u9879\u76ee\u76ee\u5f55\u4e0b\u3002\n<pre><code class=\"language-python\">TEMPLATES = [\n{\n'BACKEND': 'django.template.backends.django.DjangoTemplates',\n'DIRS': [os.path.join(BASE_DIR,'templates')],\n'APP_DIRS': True,\n'OPTIONS': {\n    'context_processors': [\n        'django.template.context_processors.debug',\n        'django.template.context_processors.request',\n        'django.contrib.auth.context_processors.auth',\n        'django.contrib.messages.context_processors.messages',\n    ],\n},\n},\n]<\/code><\/pre>\n<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>\u7b2c\u4e8c\u79cd\uff1aAPP_DIRS\u9ad8\u901f\u6a21\u677f\u5f15\u64ce\u662f\u5426\u5e94\u8be5\u8fdb\u5165\u6bcf\u4e2a\u5df2\u5b89\u88c5\u7684\u5e94\u7528\u4e2d\u67e5\u627e\u6a21\u677f\uff0c\u503c\u4e3aTrue\uff0c\u5219\u6a21\u677f\u4f1a\u53bb\u5b89\u88c5App\u590f\u76ee\u5b89\u7684templates\u6587\u4ef6\u5939\u67e5\u627e\u6a21\u677f\u3002\u6240\u4ee5\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u6bcf\u4e2aapp\u7684\u91cc\u9762\u521b\u5efa\u6a21\u677f\u76ee\u5f55templates\u5b58\u653e\u6a21\u677f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u9700\u8981\u5c06\u8fd9\u4e2aapp\u6dfb\u52a0\u5230settings.py\u6587\u4ef6\u7684INSTALLED_APPS\u5217\u8868\u4e2d\u3002<\/li>\n<\/ol>\n<h1>\u4e8c\u3001\u6a21\u677f\u53d8\u91cf<\/h1>\n<h2>1.\u6a21\u677f\u53d8\u91cf\u4f7f\u7528\u89c4\u5219<\/h2>\n<ol>\n<li>\u8bed\u6cd5\uff1a{{\u53d8\u91cf\u540d}}<\/li>\n<li>\u547d\u540d\u7531\u5b57\u6bcd\u548c\u6570\u5b57\u4ee5\u53ca\u4e0b\u5212\u7ebf\u7ec4\u6210\uff0c\u4e0d\u80fd\u6709\u7a7a\u683c\u548c\u6807\u70b9\u7b26\u53f7<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3001\u6a21\u578b(\u7c7b\u4f3c\u4e8e\u201c\u7c7b\u7684\u6982\u5ff5\u201d)\u3001\u65b9\u6cd5\u3001\u51fd\u6570\u3001\u5217\u8868<\/li>\n<li>\u4e0d\u8981\u548cpython\u6216django\u5173\u952e\u5b57\u91cd\u540d<\/li>\n<li>\u53d8\u91cf\u548c\u67e5\u627e<\/li>\n<\/ol>\n<h2>\u6ce8\uff1a<\/h2>\n<ol>\n<li>\u5982\u679cdata\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u90a3\u4e48\u8bbf\u95eedata.items\u5c06\u4f1a\u8bbf\u95eedata\u8fd9\u4e2a\u5b57\u5178\u7684key\u540d\u4e3aitems\u7684\u503c\uff0c\u800c\u4e0d\u4f1a\u8bbf\u95ee\u5b57\u5178\u7684items\u65b9\u6cd5\u3002<\/li>\n<li>\u70b9\u5728\u6a21\u677f\u6e32\u67d3\u65f6\u6709\u7279\u6b8a\u7684\u542b\u4e49\u3002\u53d8\u91cf\u540d\u4e2d\u70b9\u8868\u793a\u67e5\u627e\u3002<br \/>\n<h3>\u6a21\u677f\u53d8\u91cf\u4f20\u9012\u6570\u636e\uff1a<\/h3>\n<p><code>1.\u5217\u8868\u548c\u5b57\u5178\u4f20\u503c\uff1a\u901a\u8fc7\u70b9\u5e8f\u53f7\uff08Example:list.1\uff09\u7d22\u5f15\u53d6\u503c<\/code><\/p>\n<\/li>\n<\/ol>\n<p><strong>book\\urls.py<\/strong><\/p>\n<pre><code class=\"language-python\">from django.urls import path\nfrom . import views\n\nurlpatterns=[\n        path(&#039;index\/&#039;,views.index),\n        path(&#039;article\/&#039;, views.article),\n        path(&#039;article_new\/&#039;, views.article_new, name=&#039;article_new&#039;),\n        path(&#039;bilibili\/&#039;, views.bilibili),\n        path(&#039;temp\/&#039;, views.temp),\n        path(&#039;login\/&lt;name&gt;\/&lt;pwd&gt;&#039;,views.login)\n]<\/code><\/pre>\n<p><strong>book\\views.py<\/strong><\/p>\n<pre><code class=\"language-python\">from django.shortcuts import render,redirect,reverse\nfrom django.http import HttpResponse\nmr = Person(name=&#039;moran&#039;,age=18)\nli = [&#039;q&#039;,&#039;w&#039;,&#039;e&#039;,&#039;r&#039;]\ndir = {&#039;a&#039;:111,&#039;b&#039;:222}\n\ndef temp(request):\n    return render(request,&#039;book\/index.html&#039;,\n                  context={\n                      &#039;books_name&#039;:&#039;python&#039;,#\u5b57\u7b26\u4e32\n                      &#039;hello&#039;:hello(),#\u51fd\u6570\n                      &#039;moran&#039;:mr,#\u7c7b\u5bf9\u8c61\n                      &#039;say&#039;:mr.say,#\u65b9\u6cd5\n                      &#039;li&#039;:li,#\u5217\u8868\n                      &#039;dir&#039;:dir,#\u5b57\u5178\n                  })<\/code><\/pre>\n<p><strong>templates\\book\\index.html<\/strong><\/p>\n<pre><code class=\"language-python\">&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;\u6a21\u677f\u53d8\u91cf&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\u6b22\u8fce{{ username }}\u767b\u5f55\u7f51\u7ad9\n \u8fd9\u4e2a\u662f\u5b57\u7b26\u4e32\u53d8\u91cf{{ books_name }}&lt;br&gt;\n \u8fd9\u4e2a\u662f\u51fd\u6570\u53d8\u91cf{{ hello }}&lt;br&gt;\n \u8fd9\u4e2a\u662f\u7c7b\u53d8\u91cf{{ moran }}&lt;br&gt;\n \u8fd9\u4e2a\u662f\u7c7b\u65b9\u6cd5\u53d8\u91cf{{ say }}&lt;br&gt;\n \u8fd9\u4e2a\u662f\u5217\u8868\u53d8\u91cf{{ li.0 }}&lt;br&gt;\n \u8fd9\u4e2a\u662f\u5b57\u5178\u53d8\u91cf{{ dir.a }}&lt;br&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h1>\u4e09\u3001\u6a21\u677f \u8fc7\u6ee4\u5668<\/h1>\n<h2>1.\u8fc7\u6ee4\u5668<\/h2>\n<p>\u4f7f\u7528\uff1a{{\u53d8\u91cf | \u65b9\u6cd5 | \u65b9\u6cd5}}<\/p>\n<h4>1.1 \u4f5c\u7528<\/h4>\n<ul>\n<li>\u5bf9\u53d8\u91cf\u8fdb\u884c\u8fc7\u6ee4\u3002\u5728\u771f\u6b63\u6e32\u67d3\u51fa\u6765\u4e4b\u524d\uff0c\u8fc7\u6ee4\u5668\u4f1a\u6839\u636e\u529f\u80fd\u5904\u7406\u597d\u53d8\u91cf\uff0c\u7136\u540e\u5f97\u51fa\u7ed3\u679c\u540e\u518d\u66ff\u6362\u6389\u539f\u6765\u7684\u53d8\u91cf\uff0c\u5c55\u793a\u51fa\u6765\u3002<\/li>\n<li>\u8bed\u6cd5<code><code>{{fruits | lower}}<\/code><\/code><br \/>\n<h4>1.2 \u7ba1\u9053\u7b26\u53f7\u8fdb\u884c\u94fe\u5f0f\u8c03\u7528<\/h4>\n<\/li>\n<li>\u6bd4\u5982\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\uff0c\u5148\u628a\u6240\u6709\u5b57\u7b26\u53d8\u6210\u5c0f\u5199\uff0c\u628a\u7b2c\u4e00\u4e2a\u5b57\u7b26\u8f6c\u6362\u6210\u5927\u5199\u3002\n<ul>\n<li>\u8bed\u6cd5<code><code>{{fruits | lower | capfirst}}<\/code><\/code><br \/>\n<h4>1.3 \u4f7f\u7528\u53c2\u6570<\/h4>\n<\/li>\n<li>\u8fc7\u6ee4\u5668\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\uff0c\u5728\u8fc7\u6ee4\u5668\u540d\u79f0\u540e\u9762\u4f7f\u7528\u5192\u53f7\u201c:\u201d\uff0c\u518d\u52a0\u4e0a\u53c2\u6570\uff0c\u6bd4\u5982\u8981\u628a\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684\u7a7a\u683c\u53bb\u6389\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528cut\u8fc7\u6ee4\u5668\u3002<\/li>\n<li>\u8bed\u6cd5\u5982\u4e0b\uff1a<code><code>{{fruits | cut:&quot;&quot;}}<\/code><\/code><\/li>\n<li>\u6ce8\u610f\uff1a\u4f7f\u7528\u53c2\u6570\u7684\u65f6\u5019\uff0c\u5192\u53f7\u548c\u53c2\u6570\u4e4b\u95f4\u4e0d\u80fd\u6709\u7a7a\u683c\uff0c\u4e00\u5b9a\u8981\u7d27\u6328\u7740\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>2.\u5e38\u7528\u7684\u8fc7\u6ee4\u5668<\/h2>\n<ol>\n<li>\n<p><code>add<\/code>:\u5b57\u7b26\u4e32\u76f8\u52a0\uff0cint\u76f8\u52a0\uff0c\u5217\u8868\u76f8\u52a0\uff0c\u5982\u679c\u5931\u8d25\uff0c\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32<\/p>\n<ul>\n<li><font color=#0000FF>\u6570\u5b57\u76f8\u52a0{{ age | add:12 }}##\u76f8\u52a0\u4e8612<\/font><\/li>\n<li><font color=#0000FF>\u5b57\u7b26\u4e32\u76f8\u52a0{{ str | add:str }}<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>default<\/code> :\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u5728\u8fd9\u4e2a\u503cDjango\u8ba4\u4e3a\u662ffalse\u7684\u65f6\u5019\u4f7f\u7528\uff0c\u6bd4\u5982\uff1a\u7a7a\u5b57\u7b26\u4e32\u3001None\u3002\u533a\u522b\u4e8edefault_if_none,\u8fd9\u4e2a\u53ea\u6709\u5728\u53d8\u91cf\u4e3aNone\u7684\u65f6\u5019\u624d\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u8bbe\u7f6e\u4e86\u9ed8\u8ba4\u503c\u7684\u53d8\u91cf{{ null | default:'abcd' }}<\/li>\n<li>\n<h2>null \u53ef\u4ee5\u662f\u4efb\u4f55\u53d8\u91cf<\/font><\/h2>\n<\/li>\n<li><font color=#0000FF>\u8bbe\u7f6e\u4e86\u53ea\u6709\u4e3aNone(\u5305\u62ec\u7a7a\u5b57\u7b26\u4e32\u3001\u5217\u8868\u7b49)\u65f6\uff0c\u9ed8\u8ba4\u503c\u7684\u53d8\u91cf{{ null | default_if_none:'abcd' }}<\/li>\n<li>\n<h2>null \u53d8\u91cf\u4e3a\u7a7a\u65f6,\u8bbe\u7f6e\u9ed8\u8ba4\u503c<\/h2>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>first<\/code>:\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u5217\u8868\u7b2c\u4e00\u4e2a\u503c{{ list | first }}<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>last<\/code>:\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u503c\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u5217\u8868\u6700\u540e\u4e00\u4e2a\u503c{{ list | last }}<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>date<\/code>:\u683c\u5f0f\u5316\u65f6\u95f4\u548c\u65e5\u671f\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u8fd9\u662f\u9ed8\u8ba4\u7684date\u65e5\u671f\u65f6\u95f4\u683c\u5f0f{{ now }}<\/li>\n<li><font color=#0000FF>\u8fd9\u662f\u9ed8\u8ba4\u7684date\u65e5\u671f\u65f6\u95f4\u683c\u5f0f{{ now | date}}<\/li>\n<li><font color=#0000FF>\u8fd9\u662f\u9ed8\u8ba4\u7684time\u65e5\u671f\u65f6\u95f4\u683c\u5f0f{{ now | time}}<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u9ed8\u8ba4\u7684date\u65e5\u671f\u65f6\u95f4\u683c\u5f0f2021\u5e748\u670821\u65e5 16:51<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u9ed8\u8ba4\u7684date\u65e5\u671f\u65f6\u95f4\u683c\u5f0f2021\u5e748\u670821\u65e5<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u9ed8\u8ba4\u7684time\u65e5\u671f\u65f6\u95f4\u683c\u5f0f16:54<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>time<\/code>:\u683c\u5f0f\u5316\u65f6\u95f4<\/p>\n<ul>\n<li><font color=#0000FF>\u89c1\u4e0b3<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>join<\/code>:\u8ddfpython\u4e2d\u7684join\u4e00\u6837\u7684\u7528\u6cd5\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u8fd9\u662f\u5b57\u7b26\u4e32\u7684join\u65b9\u6cd5{{ str|join:'&amp;&amp;' }}<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u5b57\u7b26\u4e32\u7684join\u65b9\u6cd5T&amp;&amp;H&amp;&amp;I&amp;&amp;S&amp;&amp; &amp;&amp;I&amp;&amp;S&amp;&amp; &amp;&amp;P&amp;&amp;Y&amp;&amp;T&amp;&amp;H&amp;&amp;O&amp;&amp;N<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>length<\/code>\uff1a\u8fd4\u56de\u5b57\u7b26\u4e32\u6216\u8005\u662f\u6570\u7ec4\u7684\u957f\u5ea6<\/p>\n<ul>\n<li><font color=#0000FF>\u8fd9\u662f\u5b57\u7b26\u4e32\u7684\u83b7\u53d6\u957f\u5ea6\u65b9\u6cd5{{ str|length}}<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u5b57\u7b26\u4e32\u7684\u83b7\u53d6\u957f\u5ea6\u65b9\u6cd514<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>length_is<\/code>:\u5b57\u7b26\u4e32\u6216\u8005\u662f\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u5426\u662f\u6307\u5b9a\u7684\u503c\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u8fd9\u662f\u5b57\u7b26\u4e32\u7684\u5224\u65ad\u957f\u5ea6\u65b9\u6cd5{{ str|length_is:14 }}<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u5b57\u7b26\u4e32\u7684\u5224\u65ad\u957f\u5ea6\u65b9\u6cd5True<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>lower<\/code>\uff1a\u628a\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u53d8\u6210\u5c0f\u5199\u3002<\/p>\n<\/li>\n<li>\n<p><code>truncatechars<\/code> :\u6839\u636e\u540e\u9762\u7684\u53c2\u6570\uff0c\u622a\u65ad\u5b57\u7b26\uff0c\u5982\u679c\u8d85\u8fc7\u4e86\u7528...\u8868\u793a\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u8fd9\u662f\u5b57\u7b26\u4e32\u53ea\u663e\u793a\u56fa\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\uff0c\u5176\u4f59\u5185\u5bb9\u7701\u7565{{ str|truncatechars:10}}<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u5b57\u7b26\u4e32\u53ea\u663e\u793a\u56fa\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\uff0c\u5176\u4f59\u5185\u5bb9\u7701\u7565THIS IS...<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>truncatewords<\/code>:\u540ctruncatechars,\u8fd9\u662f\u4ee5\u4e00\u4e2a\u5355\u8bcd\u4e3a\u5355\u4f4d\u8fdb\u884c\u622a\u65ad\u3002<br \/>\n\u2014\u2014\u4ee5\u4e0a\u6709\u4e24\u4e2axx_html\u7c7b\u578b\u7684\uff0c\u9488\u5bf9html\uff0c\u622a\u65ad\u6807\u7b7e\u4e2d\u7684\u5b57\u7b26\uff0c\u800c\u4e0d\u4f1a\u622a\u65ad\u6807\u7b7e\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u8fd9\u662f\u5b57\u7b26\u4e32\u53ea\u663e\u793a\u56fa\u5b9a\u6570\u76ee\u7684\u5355\u8bcd\uff0c\u5176\u4f59\u5185\u5bb9\u7701\u7565{{ str|truncatewords:2}}<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u5b57\u7b26\u4e32\u53ea\u663e\u793a\u56fa\u5b9a\u6570\u76ee\u7684\u5355\u8bcd\uff0c\u5176\u4f59\u5185\u5bb9\u7701\u7565THIS IS ...<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>capfirst<\/code>:\u9996\u5b57\u6bcd\u5927\u5199\u3002<\/p>\n<\/li>\n<li>\n<p><code>slice<\/code>\uff1a\u5207\u5272\u5217\u8868\u3002\u7528\u6cd5\u8ddfpython\u4e2d\u7684\u5207\u7247\u64cd\u4f5c\u662f\u4e00\u6837\u7684\uff0c\u533a\u522b\u662f\u524d\u95ed\u5408\uff0c\u540e\u5f00\u653e\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u8fd9\u662f\u5b57\u7b26\u4e32\u5207\u7247{{ str|slice:'1:4'}}<br \/>\n\u8fd9\u662f\u5217\u8868\u5207\u7247{{ list|slice:'2:3'}}<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u5b57\u7b26\u4e32\u5207\u7247HIS<br \/>\n\u8fd9\u662f\u5217\u8868\u5207\u7247['e']<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>striptags<\/code>:\u53bb\u6389\u6240\u6709\u7684html\u6807\u7b7e\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u8fd9\u662f\u542b\u6709html\u6807\u7b7e\u7684\u5b57\u7b26\u4e32{{ html }}<br \/>\n\u8fd9\u662f\u53bb\u9664html\u6807\u7b7e\u7684\u5b57\u7b26\u4e32{{ html|striptags }}<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u542b\u6709html\u6807\u7b7e\u7684\u5b57\u7b26\u4e32\\<\/p>\n<h1>this is html.\\<\/h1>\n<p>\u8fd9\u662f\u53bb\u9664html\u6807\u7b7e\u7684\u5b57\u7b26\u4e32this is html.<\/font><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>safe<\/code>:\u5173\u95ed\u53d8\u91cf\u7684\u81ea\u52a8\u8f6c\u4e49\u3002<\/p>\n<ul>\n<li><font color=#0000FF>\u8fd9\u662f\u5173\u6389\u81ea\u52a8\u8f6c\u4e49\uff0c\u4f7f\u5b57\u7b26\u4e32\u91cc\u7684html\u6807\u7b7e\u751f\u6548{{ html|safe }}<br \/>\n=&gt;&gt;&gt;\u9875\u9762\u6548\u679c\u5982\u4e0b\uff1a<\/font><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/8da697642df34bb6b025df38c3e09924.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>floatformat<\/code>:\u6d6e\u70b9\u6570\u683c\u5f0f\u5316<\/p>\n<ul>\n<li><font color=#0000FF>\u8fd9\u662f\u6ca1\u505a\u5904\u7406\u7684\u5c0f\u6570{{ float }}<br \/>\n\u8fd9\u662f\u4fdd\u7559\u4e00\u4f4d\u7684\u5c0f\u6570{{ float|floatformat }}<br \/>\n\u8fd9\u662f\u4fdd\u7559\u6307\u5b9a\u4f4d\u6570\u7684\u5c0f\u6570{{ float|floatformat:'4' }}<br \/>\n=&gt;&gt;&gt;\u8fd9\u662f\u6ca1\u505a\u5904\u7406\u7684\u5c0f\u65703.1415926<br \/>\n\u8fd9\u662f\u4fdd\u7559\u4e00\u4f4d\u7684\u5c0f\u65703.1<br \/>\n\u8fd9\u662f\u4fdd\u7559\u6307\u5b9a\u4f4d\u6570\u7684\u5c0f\u65703.1416<\/font><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>3.date\u548ctime\u8fc7\u6ee4\u5668\u683c\u5f0f:<\/h2>\n<ol>\n<li><code>Y<\/code>:\u56db\u4f4d\u6570\u7684\u5e74\u3002\u5982\uff1a1999<\/li>\n<li><code>y<\/code>:\u4e24\u4f4d\u6570\u7684\u5e74\u3002\u598299<\/li>\n<li><code>m<\/code>:\u4e24\u4f4d\u6570\u7684\u6708\u3002\u5982\uff1a02<\/li>\n<li><code>n<\/code>:\u4e00\u4f4d\u6570\u7684\u6708\u3002\u5982\uff1a2<\/li>\n<li><code>d<\/code>:\u4e24\u4f4d\u6570\u7684\u65e5\u3002\u5982\uff1a01<\/li>\n<li><code>j<\/code>:\u4e00\u4f4d\u6570\u7684\u65e5\u3002\u5982\uff1a1<\/li>\n<li><code>g<\/code>:12\u5c0f\u65f6\u5236\u7684\u4e00\u4f4d\u6570\u7684\u5c0f\u65f6\u3002\u5982\uff1a1,9,12<\/li>\n<li><code>G<\/code>:24\u5c0f\u65f6\u5236\u7684\u4e00\u4f4d\u6570\u5c0f\u65f6\u3002\u5982\uff1a0,8\uff0c23<\/li>\n<li><code>h<\/code>:12\u5c0f\u65f6\u5236\u7684\u4e24\u4f4d\u6570\u7684\u5c0f\u65f6\u3002\u598201,09,12<\/li>\n<li><code>H<\/code>\uff1a24\u5c0f\u65f6\u7684\u4e24\u4f4d\u6570\u7684\u5c0f\u65f6\u3002\u5982\uff1a01,13,24<\/li>\n<li><code>i<\/code>:\u5206\u949f\u3002\u4ece00-59<\/li>\n<li><code>s<\/code>:\u79d2\u3002\u4ece00-59<\/li>\n<\/ol>\n<p><strong>app templates\\book\\index.html<\/strong><\/p>\n<pre><code class=\"language-python\">&lt;!DOCTYPE html&gt;&lt;html lang=&quot;en&quot;&gt;&lt;head&gt;&lt;meta charset=&quot;UTF-8&quot;&gt;&lt;title&gt;\u6a21\u677f\u53d8\u91cf&lt;\/title&gt;&lt;\/head&gt;\n&lt;body&gt;\n{#\u6ce8\u610f**date:**\u90e8\u5206\u5594\uff0c\u5192\u53f7\u540e\u9762\u4e0d\u80fd\u6709\u7a7a\u683c\uff01#}\n\u8fd9\u662fdate\u8fc7\u6ee4\u5668\u7684\u5e74\u6708\u65e524\u5c0f\u65f6\u5236\u683c\u5f0f{{ now | date:&#039;Y-m-d H:i:s&#039; }}&lt;br&gt;\n\u8fd9\u662fdate\u8fc7\u6ee4\u5668\u7684\u5e74\u6708\u65e512\u5c0f\u65f6\u5236\u683c\u5f0f{{ now | date:&#039;Y-m-d h:i:s&#039; }}&lt;br&gt;\n&lt;\/body&gt;&lt;\/html&gt;<\/code><\/pre>\n<p><font color=#0000FF>=&gt;&gt;\u8fd9\u662fdate\u8fc7\u6ee4\u5668\u7684\u5e74\u6708\u65e524\u5c0f\u65f6\u5236\u683c\u5f0f2021-08-21 17:19:46<br \/>\n\u8fd9\u662fdate\u8fc7\u6ee4\u5668\u7684\u5e74\u6708\u65e512\u5c0f\u65f6\u5236\u683c\u5f0f2021-08-21 05:21:13<\/font><\/p>\n<h2>4.\u8fc7\u6ee4\u5668\u7684\u4f8b\u5b50<\/h2>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/71ae52d9c72845038aefd539f044c8f0.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/24576205f2d94255808956be663f3c3f.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/f82456679049484098b8ed5d2d89034f.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>5.\u5173\u4e8e\u81ea\u52a8\u8f6c\u4e49<\/h2>\n<ul>\n<li>\u4ec0\u4e48\u662f\u81ea\u52a8\u8f6c\u4e49\uff1f<br \/>\n\u2014\u2014  \u81ea\u52a8\u8f6c\u4e49\u662f\u5c06\u53d8\u91cf\u7684\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\uff0c\u6bd4\u5982\u5de6\u7bad\u5934\uff08&lt;\uff09\u3001\u53f3\u7bad\u5934\uff08&gt;\uff09\u8f6c\u4e49\u6210html\u4ee3\u7801\uff0c\u8fd9\u6837\u505a\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u5904\u7406\u4e00\u4e9b\u4e0d\u5b89\u5168\u7684\u53d8\u91cf\u3002<\/p>\n<ul>\n<li>&lt;\uff1a\u8f6c\u4e49\u6210\\&lt;<\/li>\n<li>\n<blockquote>\n<p>\uff1a\u8f6c\u4e49\u6210\\&gt;<\/p>\n<\/blockquote>\n<\/li>\n<li>\u2018\uff1a\u8f6c\u4e49\u6210\\&#39;<\/li>\n<li>\u201c\uff1a\u8f6c\u4e49\u6210\\&quot;<\/li>\n<li>&amp;\uff1a\u8f6c\u4e49\u6210\\&amp;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>app book\\views.py<\/strong><\/p>\n<pre><code class=\"language-python\">from django.shortcuts import render\nfrom django.http import HttpResponse\ndef index_1(request):\n    return render(request,&#039;book\/index.html&#039;,\n                  context={\n                      &#039;a&#039;: &#039;&amp;lt&#039;\n                  })<\/code><\/pre>\n<p><strong>templates\\book\\index_1.html<\/strong><\/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;\u6a21\u677f\u53d8\u91cf&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\u8fd9\u662f\u81ea\u52a8\u8f6c\u4e49\u7684\u5b57\u7b26{{ a| safe }}&lt;br&gt;\n\u8fd9\u662f\u81ea\u52a8\u8f6c\u4e49\u7684\u5b57\u7b26{{ a }}&lt;br&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p><strong>\u9875\u9762\u9884\u89c8<\/strong><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/fd15b63672b844b79cde0e0d55835fd9.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h1>\u56db\u3001\u9759\u6001\u6587\u4ef6\u5f15\u7528<\/h1>\n<h2>1.\u9759\u6001\u6587\u4ef6\u76ee\u5f55<\/h2>\n<ul>\n<li><strong>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efastatic\u7684\u76ee\u5f55, \u4e3a\u4e86\u533a\u5206\u5f00\u5404\u79cd\u7c7b\u578b\u7684\u6587\u4ef6\u5206\u522b\u521b\u5efacss,image,js\u7684\u76ee\u5f55.<\/strong><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/cb02a3a1100c489c89652935bb6b6023.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>2. \u9759\u6001\u6587\u4ef6\u7684\u914d\u7f6e<\/h2>\n<ul>\n<li><strong>\u5728settings.py\u6587\u4ef6\u4e2d\u6dfb\u52a0STATICFILES_DIRS<br \/>\n\u8bbe\u7f6e\u9759\u6001\u6587\u4ef6\u76ee\u5f55\u8def\u5f84\uff0c\u540ctemplates<\/strong><\/p>\n<pre><code class=\"language-python\">STATIC_URL = &#039;\/static\/&#039;\nSTATICFILES_DIRS = [\nos.path.join(BASE_DIR,&#039;static&#039;)##BASE_DIR:\u4e3b\u6587\u4ef6\u5939\n]<\/code><\/pre>\n<h2>3.\u521b\u5efa\u9759\u6001\u6587\u4ef6<\/h2>\n<\/li>\n<\/ul>\n<\/li>\n<li>1.\u521b\u5efa\u6587\u4ef6\/static\/js\/myjs.js<br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/681f4caf10074ef08675dd70d0080943.png\" alt=\"-\" \/><\/li>\n<li>2.\u521b\u5efa\u6587\u4ef6\/static\/css\/mystyle.css<br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/27dac2b3e9c146f3aeeed8f420cd5258.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<ul>\n<li>3.\u521b\u5efa\u6587\u4ef6\/static\/image\/myimage.jpg<br \/>\n<code><code>##\u5b58\u5165\u56fe\u7247<\/code><\/code><\/li>\n<li>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/c8fb270a7e2d4a35b2b51c2c2e722b07.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>3.\u9759\u6001\u6587\u4ef6\u5f15\u7528<\/h2>\n<p><strong>\u521b\u5efa\u6a21\u677f\/templates\/book\/static_test.html<\/strong><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/ae597fc8e480412c9faa419d0b196d65.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>\u603b\u7ed3<\/h1>\n<p>&lt;hr style=&quot; border:solid; width:100px; height:1px;&quot; color=#000000 size=1&quot;&gt;<\/p>\n<p>1\u3001 \u6a21\u677f\u8def\u5f84<br \/>\n2\u3001 \u6a21\u677f\u53d8\u91cf<br \/>\n3\u3001 \u8fc7\u6ee4\u5668\u7684\u6982\u5ff5<br \/>\n4\u3001 \u8fc7\u6ee4\u5668\u7684\u4f7f\u7528<br \/>\n5\u3001 \u9759\u6001\u6587\u4ef6\u5f15\u7528<br \/>\n&lt;hr style=&quot; border:solid; width:100px; height:1px;&quot; color=#000000 size=1&quot;&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6a21\u677f\u8def\u5f84\u603b\u7ed3 \u5728\u914d\u7f6e\u6587\u4ef6settings.py\u4e2d\u627e\u5230TEMPLATES\u8bbe\u7f6e\u6765\u914d\u7f6e \u8fd9\u662f\u4e00\u4e2a\u8bbe\u7f6e\u9009\u9879\u7684\u5217\u8868\uff0c\u6a21\u677f\u5927\u90fd\u5305\u542b\u4e24\u9879\u901a\u7528\u8bbe\u7f6e\uff1b   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[26],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/311"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=311"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":1500,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/311\/revisions\/1500"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}