1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Django(二):templates模板+静态资源目录static

Django(二):templates模板+静态资源目录static

时间:2022-11-22 23:51:05

相关推荐

Django(二):templates模板+静态资源目录static

文章目录

一、templates模板1.模板标签系统介绍2.调用模板的三种方法3.模板语法①取列表和字典的值②条件语句if和循环语句for③过滤器 二、静态资源目录static1.静态目录的作用2.静态文件的配置3.使用静态资源文件4.静态文件的两种调用方式

一、templates模板

1.模板标签系统介绍

在做web开发,要给用户提供一个页面,页面包括静态页面+数据,两者结合起来就是完整的可视化的页面,django的模板系统支持这种功能,首先需要写一个静态页面(结构html,样式css,行为js)然后通过python的模板语法将数据渲染上去。

1.创建一个templates目录

2.配置模板(使用os模块的拼接)

配置项介绍

2.调用模板的三种方法

views页面

# ---------------------------------调用templates的方法一-----------------from django.shortcuts import renderfrom django.http import HttpResponsedef indextmp(request):# render的三个参数(请求,页面,字典参数),字典参数对应页面的用{{key}}return render(request, 'indextmp.html', {'content': '哈哈哈哈哈'})# ---------------------------------调用templates的方法二-----------------from django.shortcuts import render_to_responsedef a(request):# render_to_response两个参数(页面,字典参数),字典参数对应页面的用{{key}}return render_to_response('indextmp.html', {'content': '哈哈哈哈哈'})# ---------------------------------调用templates的方法三-----------------from django.template.loader import get_templatedef 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>

3.模板语法

遵循python语法,但是需要有开始和终结符号

①取列表和字典的值

取列表值在python中为list[索引值],在html中为list.索引值取字典值在python中为d[key值],在html中为d.key值

views.py

# locals()会将局部变量都转换为字典,不需要自己转换为字典def d(request):name = '张三'age = 12hobby = ['唱', '跳', '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>

运行结果

②条件语句if和循环语句for

views.py

# 模板语法的if使用def e(request):name = '张三'age = 33hobby = ['唱', '跳', '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

<!DOCTYPE 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>

③过滤器

基本格式:{{ 变量名 | 过滤器:可选参数 }}

举例:

二、静态资源目录static

1.静态目录的作用

在web开发过程当中,有一类型的文件专门存放静态资源(例如css,js,image)的静态文件。这些文件通常不被直接访问,往往是在加载页面的时候被加载,这些内容是固定的。我们一般在固定文件夹中存放这些静态资源,一般是static目录

2.静态文件的配置

①创建static目录

②在static目录下创建三个目录

③配置静态文件setting

# 静态文件的配置STATIC_URL = '/static/'STATICFILES_DIRS = (os.path.join(BASE_DIR,'static'),)#STATICFILES_DIRS 后面可以是列表或者是元组

3.使用静态资源文件

views.py

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:

<!DOCTYPE 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;}

结果:

4.静态文件的两种调用方式

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。