寫在前面 最近看到稀土掘金在搞2023年終總結徵文活動,一直想嘗試投稿試試,周末我就花了近一下午時間寫完初稿,然後周一、周二完成精讀再改稿,感覺OK,昨晚凌晨第一時間在稀土掘金投稿。 結果,又發生了同樣的事情。 同樣的文章,在博客園上、公號上閱讀量很OK,在稀土掘金上就上不來。 這應該是我博客園第四 ...
寫在前面
最近看到稀土掘金在搞2023年終總結徵文活動,一直想嘗試投稿試試,周末我就花了近一下午時間寫完初稿,然後周一、周二完成精讀再改稿,感覺OK,昨晚凌晨第一時間在稀土掘金投稿。
結果,又發生了同樣的事情。
同樣的文章,在博客園上、公號上閱讀量很OK,在稀土掘金上就上不來。
這應該是我博客園第四次上榜一了把,也是是第二次找到運營大大了,我都有點不好意思了,我好不容易鼓起勇氣想投個稿,結果尷尬,不過後來運營大大也幫忙上推薦了,但是值得一說的是嗎,這個掘金閱讀量上不來,是我寫的不行嗎?回頭我好好總結下,做個復盤。
先不想了,還是接著更文吧。
模板中標簽的使用
Django
的MVC
思想,模板是一個文本,用於分離文檔的表現形式和內容,而不是將數據與視圖混合在一起
先創建靜態文件,在mycontent/templates/
下創建template_demo.html
,暫時先不修改如內容,在django_project/settings.py
,修改為如下代碼:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR+"/mycontent/templates",], # 預設為[]
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
1、模板標簽變數的使用
template_demo.html
添加如下代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TEMPLATE_DEMO</title>
</head>
<body>
<p>variables_demo: {{ name }}</p>
</body>
</html>
在mycontent/views.py
中添加代碼如下:
def variables_demo(request):
name = 'variables_demo'
return render(request, 'template_demo.html', {'name': name})
在django_project/urls.py
中添加代碼如下:
urlpatterns = [
path("index/", views.index),
re_path('^$', views.index),
url('^$', views.index),
url('^my_content/$', views.my_content),
url('^my_content/page=(\d+)$', views.pagehelper),
url('variables_demo/', views.variables_demo)
]
效果如下:
2、模板標簽列表的使用
template_demo.html
添加如下代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TEMPLATE_DEMO</title>
</head>
<body>
<p>variables_demo: {{ name }}</p>
<p>list_demo: {{ list }}</p>
</body>
</html>
在mycontent/views.py
中添加代碼如下:
def list_demo(request):
list = ['list1','list2','list3']
return render(request, 'template_demo.html', {'list': list})
在django_project/urls.py
中添加代碼如下:
urlpatterns = [
path("index/", views.index),
re_path('^$', views.index),
url('^$', views.index),
url('^my_content/$', views.my_content),
url('^my_content/page=(\d+)$', views.pagehelper),
url('variables_demo/', views.variables_demo),
url('list_demo/', views.list_demo)
]
效果如下:
3、模板標簽字典的使用
template_demo.html
添加如下代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TEMPLATE_DEMO</title>
</head>
<body>
<p>variables_demo: {{ name }}</p>
<p>list_demo: {{ list }}</p>
<p>dict_demo: {{ dict }}</p>
</body>
</html>
在mycontent/views.py
中添加代碼如下:
def dict_demo(request):
dict_demo = {'dict': 'dict_demo'}
return render(request, 'template_demo.html', {'dict': dict_demo})
在django_project/urls.py
中添加代碼如下:
urlpatterns = [
path("index/", views.index),
re_path('^$', views.index),
url('^$', views.index),
url('^my_content/$', views.my_content),
url('^my_content/page=(\d+)$', views.pagehelper),
url('variables_demo/', views.variables_demo),
url('list_demo/', views.list_demo),
url('dict_demo/', views.dict_demo),
]
效果如下:
知識點:
變數模板語法:
- view:{"HTML變數名" : "views變數名"}
- HTML:{{變數名}}
4、模板的一些特殊慄子
template_demo.html
添加如下代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TEMPLATE_DEMO</title>
</head>
<body>
<p>variables_demo: {{ name|safe }}</p>
<!-- 過濾器部分例子-->
<p>過濾器部分例子: {{ name|upper }}</p>
<!-- length例子-->
<p>length例子: {{ name|length }}</p>
<!-- 截斷的字元串將以 ... 結尾。-->
<p>截斷的字元串將以 ... 結尾: {{ name|truncatechars:2}}</p>
<p>list_demo: {{ list }}</p>
<p>list_demo: {{ list|first|upper }}</p>
<p>dict_demo: {{ dict }}</p>
</body>
</html>
在mycontent/views.py
中添加代碼如下:
def variables_demo(request):
name = '<script>alert("safe test!")</script>'
return render(request, 'template_demo.html', {'name': name})
效果如下:
知識點:
{{ views_str|safe }}
:將字元串標記為安全,不需要轉義,要保證 views.py
傳過來的數據絕對安全,才能用 safe
。
5、if/else、for標簽、遍歷字典的使用
if/else標簽:
基本語法格式如下:
{% if condition %}
... display
{% endif %}
# 或者:
{% if condition1 %}
... display 1
{% elif condition2 %}
... display 2
{% else %}
... display 3
{% endif %}
根據條件判斷是否輸出。if/else
支持嵌套。
{% if %}
標簽接受 and
,or
或者 not
關鍵字來對多個變數做判斷 ,或者對變數取反( not ),例如:
{% if athlete_list and coach_list %}
athletes 和 coaches 變數都是可用的。
{% endif %}
for 標簽使用:
基本語法格式如下:
{% for athlete in athlete_list %}
{{ athlete.name }}
{% endfor %}
示例:
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% endfor %}
</ul>
遍歷字典的使用
{% for i,j in views_dict.items %}
{{ i }}---{{ j }}
{% endfor %}
效果如下:
6、註釋標簽
Django
註釋使用 {# #}。
{# 這是一個註釋 #}
寫在最後
感覺有點像Java
中的httl
、jsp
都有點像呢,馬上熟悉感就來了,作為一個Java
黨,這突然而來的熟悉感,真的好親戚,哈哈!
優秀不夠,你是否無可替代
軟體測試交流QQ群:721256703,期待你的加入!!
歡迎關註我的微信公眾號:軟體測試君
