1、创建Django工程
1
|
django-admin startproject 工程名 |
2、创建App
1
2
|
cd 工程名
python manage.py startapp cmdb
|
3、静态文件配置
编辑 project.settings.py,追加以下内容
1
2
3
|
STATICFILES_DIRS = ( os.path. join (BASE_DIR, "static" ),
) |
4、模板路径配置
1
|
DIRS ==> (os.path. join (BASE_DIR, 'templates' ),)
|
5、settings中,注释 csrf
1
2
3
|
MIDDLEWARE_CLASSES = ( #'django.middleware.csrf.CsrfViewMiddleware',
) |
6、定义路由规则,在url.py中定义用户访问的url由哪个函数来处理请求
1
2
|
url.py "login" --> 函数名
|
7、定义视图函数,在views.py中定义函数
1
2
3
4
5
6
7
8
|
def func(reuest): # request.method GET/POST
# http://127.0.0.1:8000/home?nid=123&name=bob
# request.GET.get('',None) #获取请求发来的数据
# request.POST.get('',None)
# return HttpResponse("字符串")
# return Render(request, 'HTML模板路径',{"user_list": USER_LIST})
# return redirect('只能填URL,例如:/home')
|
8、模板渲染
Django特殊的模板语言,例如:
--变量名取值--
在views.py中定义好变量和对应的值,在template的html页面中通过` var_name `的方式来取值
1
2
3
4
5
6
7
8
9
10
|
def func(request): return render(request, "index.html" ,{ 'current_user' : "bob" })
index.html <html> <body> <div>{{ current_user }}< /div >
< /body >
< /html >
|
--For循环取值--
在views.py中定义好列表和值,在template的html页面中通过{{ for循环 }}的方式来取值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
def func(request): return render(request, "index.html" ,{
'current_user' : "bob" ,
'user_list' :[ 'bob' , 'aaa' , 'bbb' ]})
index.html <html> <body> <div>{{ current_user }}< /div >
<ul>
{% for row in user_list %}
<li>{{ row }}< /li >
<% endfor %>
< /ul >
< /body >
< /html >
|
--索引取值--
在views.py中定义好字典和值,在template的html页面中通过` var_name`.`key `的方式来取值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
def func(request): return render(request, "index.html" ,{
'current_user' : "bob" ,
'user_list' : [ 'bob' , 'aaa' , 'bbb' ],
'user_dict' : [ 'k1' : 'v1' , 'k2' : 'v2' ]
})
index.html <html> <body> <div>{{ current_user }}< /div >
<a>{{ user_list.1 }}< /a > // 对于列表直接用索引取值
<a>{{ user_dict.k1 }}< /a > // 对于字典,要用key取值
<a>{{ user_dict.k2 }}< /a >
< /body >
< /html >
|
--循环字典--
模板语法除了而已循环列表外,也可以循环字典,用法和python基本一样,只不过循环时,不要加括号()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
USER_DICT = { 'K1' : 'V1' ,
'K2' : 'V2' ,
'K3' : 'V3' ,
'K4' : 'V4'
} 例1、循环key {% for k in USER_DICT.keys %}
<li>{{ k }}< /li >
{% endfor %} 例2、循环键值对 {% for item in USER_DICT.items %}
<li>{{ item }}< /li >
{% endfor %} 例3、循环values {% for v in USER_DICT.values %}
<li>{{ v }}< /li >
{% endfor %} 例4、循环values {% for k, v in USER_DICT.items %}
<li>{{ k }}-{{ v }}< /li >
{% endfor %} |
--条件取值--
{% if %}
{% if %}
{% endif %}
{% else%}
{% else %}
{% endif %}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
def func(request): return render(request, "index.html" ,{
'current_user' : "bob" ,
'age' : 18,
'user_list' : [ 'bob' , 'aaa' , 'bbb' ],
'user_dict' : [ 'k1' : 'v1' , 'k2' : 'v2' ]
})
index.html <html> <body> <div>{{ current_user }}< /div >
<a>{{ user_list.1 }}< /a > // 对于列表直接用索引取值
<a>{{ user_dict.k1 }}< /a > // 对于字典,要用key取值
<a>{{ user_dict.k2 }}< /a >
{% if age %}
<a>有年龄< /a >
{% if age > 16 %}
<a>老男人< /a >
{% else %}
<a>小鲜肉< /a >
{% endif %}
{% else %}
<a>无年龄< /a >
{% endif %}
< /body >
< /html >
|
模板的工作流程:
-
1、当用户请求通过url发送请求后,django路由将请求交给视图函数,
-
2、然后视图函数会先去取模板,
-
3、将模板中有` current_user `这样的变量与视图中定义的值进行渲染,渲染后将模板内容转换成字符串
-
4、将生成后字符串整体返回给用户
过滤器
模板过滤器是变量显示前转换它们的值的方式,看起来像下面这样
1
|
{{ name|lower }} |
这将显示通过lower过滤器过滤后` name `变量的值,它将文本转换成小写
使用(|)管道来申请一个过滤器
过滤器可以串成链,即一个过滤器的结果可以传向下一个,下面是escape文本内容然后把换行转换成p标签的习惯用法
1
|
{{ my_text|escape|linebreaks }} |