Django模版
模版默認在每一個app下templates目錄下尋找;
可以在setting裏面的templates下dirs自定義路徑
from django.shortcuts import render,render_to_response
from django import template
from django.template.loader import get_template,render_to_string
1:
t = template.Template('<html><head></head><body>{{title}}</body></html'>
c = template.Context({'title':'這是標題A'})
html = t.render(c)
return HttpResponse(html)
2:
t = get_template('books.html')
html = t.render()
return HttpResponse(html)
3:
html = render_to_string('books.html')
return HttpResponse(html)
4:
return render_to_response('books.htm;')
5(常用!):
render(requests,index.html,{name:123} 第二個參數html頁面名字,第三個可傳遞參數到頁面上{{}}表示
模版變量過濾器
默認尋找html文件都是在每一個app下面的templates目錄下尋找
html中模版變量用{{}}代替,例如return render('index.html',{'name':123} {{name}}
#過濾器:
在所有內容渲染之前做一層處理:
語法:
{{變量或其他|語法}} 支持多層管道
注意事項:
千萬不要用兩個管道||這個在其他語言例如JavaScript爲or
#常用方法(注意:這些所有的方法,都是在eturn render 裏面的變量進行測試的):
length:長度
add:字符串相加,數字相加,列表,如果失敗,會返回空字符串 {{變量|add: "abc"}}
upper:轉換成大寫 {{變量|upper}}
lower:轉換成小寫 {{變量|lower}}
capfirst:首字母大寫 {{變量|lower|capfirst}}
cut:切割 {{name|cut:" "}} 將空格切掉
default:提供一個默認值,變量爲false or '' 使用 {{變量|default:11111}}
first:返回列表的第一個值
last:返回列表的最後一個值
join:連接字符串 {{變量|join:','}}
truncatechars:切斷超過多少個就會用...代替 {{變量|truncatechars:4}} 默認...佔三個
truncatewords:截取單詞 {{變量|truncatewords:4}}
slice:切割列表
striptags:將html標籤去掉 {{變量 | striptags}}
safe:關閉變量的自動轉譯,如果跟上safe那麼html標籤會生效 {{<h1>大標籤</h1>| safe}}
date:格式化日期:注意格式化的時候變量要爲時間,例如(變量:datetime.datetime.now())
{{name|date}} {{name|date:'Y-m-d-G:H:i:s'}}
time:格式化時間:
只能用時間 小時,日,分,秒等
date和time過濾器格式:
Y: 四位數字的年 如1998
y: 兩位數字的年 如98
m: 兩位數的月 如07
n: 一位數的月 如7
d: 兩位數的日 如07 29
j: 一位數的日 如7 29
g: 12小時制的一位數的小時 如7 2 9
G: 24小時制的一位數小時 如0 7 23
h: 12小時制的兩位數的小時 如07 01 12
H: 24小時制的兩位數的小時 如01 13 24
i: 分鐘 從00-59
s: 秒鐘 從00-59
#模版標籤:{%tag%}{%endtag%}:
if/elif/else
and/or/in/not/==/!=/<=/>=
for in 跟python語法類似
lood:加載第三方標籤最常用 加載前端文件 {%load static%}
url:返回一個命名了的url絕對路徑 "{% url 'start' article_id='1' comment_id='2' %}"
with:緩存一個變量 {% with name as c %} <h2>{{c}}</h2>{% endwith %}
autoescape:開啓和關閉自動轉譯
forloop.counter 當前迭代的次數 下標從1開始
forloop.countero 當前迭代的次數 下標從0開始
forloop.revcounter 跟forloop.counter一樣下標從大到小
forloop.revcounterO 跟forloop.counterO一樣 下標從大到小
forloop.first 返回bool類型 如果是第一次迭代 返回true 否則返回false
forloop.last 返回bool類型 如果是最後一次次迭代 返回true 否則返回false
forloop.parentloop 如果發生多層for循環嵌套,那麼這個變量返回的是上一層的for
<ul>
{% for i in name %}
{% if forloop.first %}
<li>{{i}} true</li>
{% else %}
<li>{{i}}</li>
{% endif %}
{% endfor %}
</ul>
#模版繼承:
{% extends '父模版名字' %} 需要在html最上面
{% block 自定義名稱 %}{% endblock 自定義名稱 %}父模版開放接口 子模版調用進行編輯,如果不定義那麼,子模版中無法自定義編輯其他內容,只能繼承父模版內容
{{block.super}} 如果在父模版定義的block接口中 定義了父模版的一些內容,那麼在子模版調用block接口的時候,父模版接口中的內容不存在,這個時候,需要調用如上方法
#include:
可以包含一個html模版到當前模版中,和繼承不同,include相當於把include的文件拷貝一份到當前位置,例如有一個includetemplate.html模版,要在subtemplaee.html模版中進行飲用,代碼如下:
{% extends 'base.html' %}
{% block bodyblock %}
這是博客詳情
<br/>
{% include "includetempplate.html" %}
{% endblock %} 這段代碼是 直接將includetemplates 文件裏面的內容直接複製進來
{% comment %}{% endcomment %} 註釋
#static 靜態文件:
在每一個app下默認搜索static目錄
{% load static %} html文件加載static
<link rel="stylesheet" href="{% static 'index.css' %}"> 加載css 尋找static/index.css