在做web开发,要给用户提供一个页面,页面包括静态页面+数据,两者结合起来就是完整的可视化的页面,django的模板系统支持这种功能,首先需要写一个静态页面(结构html,样式css,行为js)然后通过python的模板语法将数据渲染上去。
1.创建一个templates目录
2.配置模板(使用os模块的拼接)
配置项介绍
views页面
# ---------------------------------调用templates的方法一-----------------
from django.shortcuts import render
from django.http import HttpResponse
def indextmp(request):
# render的三个参数(请求,页面,字典参数),字典参数对应页面的用{{key}}
return render(request, 'indextmp.html', {'content': '哈哈哈哈哈'})
# ---------------------------------调用templates的方法二-----------------
from django.shortcuts import render_to_response
def a(request):
# render_to_response两个参数(页面,字典参数),字典参数对应页面的用{{key}}
return render_to_response('indextmp.html', {'content': '哈哈哈哈哈'})
# ---------------------------------调用templates的方法三-----------------
from django.template.loader import get_template
def b(request):
template = get_template('indextmp.html')
con = {'content': '哈哈哈哈哈'}
hh = template.render(con)
return HttpResponse(hh)
urls.py
path('indextmp/',views.indextmp),
path('a/',views.a),
path('b/',views.b),
templates模板里,创建一个indextmp.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>体验模板</title>
</head>
<body>
<h1>11111111111111111111111111111</h1>
<h2>{{content}}</h2>
</body>
</html>
遵循python语法,但是需要有开始和终结符号
views.py
# locals()会将局部变量都转换为字典,不需要自己转换为字典
def d(request):
name = '张三'
age = 12
hobby = ['唱', '跳', 'rap']
course = {'yw': 55, 'sx': 66, 'yy': 100}
return render(request, 'test1.html', locals())
test1.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
姓名:{{ name }}<br>
年龄:{{ age }}<br>
爱好:{{ hobby.0 }}<br>
成绩:{{ course.sx }}
</body>
</html>
views.py
# 模板语法的if使用
def e(request):
name = '张三'
age = 33
hobby = ['唱', '跳', 'rap']
course = {'yw': 55, 'sx': 66, 'yy': 100}
return render(request, 'test1.html', locals())
#—————————————————————————————————————————————
# 模板语法的for使用
def g(request):
return render(request, 'test1.html', {'name': '张三', 'age': 12, 'hobby': ['唱', '跳', 'rap'],
'course': {'yw': 55, 'sx': 66}, 'yy': 100})
#—————————————————————————————————————————————
# 模板语法的for使用,forloop负责计数次数
def h(request):
return render(request, 'test1.html', {'name': '张三', 'age': 12, 'hobby': ['唱', '跳', 'rap'],
'course': {'yw': 55, 'sx': 66}, 'yy': 100})
test1.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试title>
head>
<body>
{% if age > 15 and age < 30 %}
<p>我是青年p>
{% elif age >= 30 %}
<p>我是老年p>
{% endif %}
{% for x in hobby reversed %}
<p>{{ x }}p>
{% endfor %}
{% for x in hobby reversed %}
<p>{{forloop.counter}}.{{ x }}p>
{% endfor %}
#----------------------------------倒序且计数
{% for x in hobby reversed %}
{% if forloop.first %}
<p style="color: red">{{forloop.counter}}.{{ x }}p>
{% elif forloop.last %}
<p style="color: blue">{{forloop.counter}}.{{ x }}p>
{% else %}
<p>{{forloop.counter}}.{{ x }}p>
{% endif %}
{% endfor %}
body>
html>
基本格式:{{ 变量名 | 过滤器:可选参数 }}
举例:
功能 | 举例 | 意义 |
---|---|---|
小写 | {{ name|lower }} |
对name进行小写转换,前端显示为小写字符 |
大写 | {{ name |upper }} |
对name进行大写转换,前端显示为大写字符 |
传递后大写 | {{ my_list|first|upper }} |
对my_list的第一个元素进行大写转换 |
截取词 | {{ bio|truncatewords:"30" }} |
将显示变量 bio 的前30个词。 |
默认值 | {{ name|default:"默认值" }} |
当后端传入的词为false的时候,自动替换为默认值。(0, 0.0, False, "", [], () , set(), {} None) |
求长度 | {{ name|length}} |
展示name的长度 |
根据字符截取长度 | {{ str|truncatechars:2}} |
只展示str的前两个字符,后边的字符用…展示 |
安全转义 | {{ views_str|safe }} |
Django 会自动对 views.py 传到HTML文件中的标签语法进行转义,令其语义失效。加 safe 过滤器是告诉 Django 该数据是安全的,不必对其进行转义,可以让该数据语义生效。 |
在web开发过程当中,有一类型的文件专门存放静态资源(例如css,js,image)的静态文件。这些文件通常不被直接访问,往往是在加载页面的时候被加载,这些内容是固定的。我们一般在固定文件夹中存放这些静态资源,一般是static
目录
②在static目录下创建三个目录
# 静态文件的配置
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
#STATICFILES_DIRS 后面可以是列表或者是元组
def statictest(request):
phb = [
{'name': '吕布', 'img': 'lvbu.png'},
{'name': '赵云', 'img': 'zy.png'},
{'name': '关羽', 'img': 'gy.png'},
]
return render(request, 'statictest.html', locals())
urls.py
path('statictest/',views.statictest),
statictest.html:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试静态文件title>
<link rel="stylesheet" href="../static/css/statictest.css">
head>
<body>
{% for one in phb %}
<img src="../static/images/{{ one.img }}">
<h1>
姓名:{{ one.name }}
h1>
<hr>
{% endfor %}
body>
html>
statictest.css
img{
width: 160px;
height: 160px;
}
h1{
display: inline-block;
}