变量传值
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
# 获取登录的用户名
username = request.GET.get('username')
if username:
# 如果想把值传到Templates中, 必须传递字典类型
# {‘user’:username} -- user: 在模板中通过这个名称访问, username: 具体传过去的值
content = {'user': username}
return render(request, 'index.html', context=content)
else:
return HttpResponse("没有用户登录!!!")
def index02(request):
# 传多个值:学号,姓名,性别,出生日期
sno = '95001'
name = '张三'
gender = '男'
birthday = '1990-10-10'
# 把多个值拼接成字典类型
content = {'sno': sno,'name':name, 'gender': gender, 'birthday':birthday}
# 加载HTML时候附带数据
return render(request, 'index02.html',context=content)
def index03(request):
# 传递List集合
alice = ['95001', 'alice', '女', '1990-10-10']
return render(request,'index03.html', context={'student':alice})
def index04(request):
# 传递的是字典类型
alice = {'sno':'95001', 'name':'alice', 'gender':'女', 'birthday':'1990-10-10'}
return render(request, 'index04.html', context={'student': alice})
# 定义个类
class Student:
def __init__(self,sno,name,gender,birthday):
self.sno = sno
self.name = name
self.gender = gender
self.birthday = birthday
def index05(request):
# 传递一个对象
alice = Student('98008','张三','男','1998-9-8')
return render(request, 'index05.html', context={'student': alice})
分别对应着前端的html
<body>
<div id="title">模板语言变量传值</div>
<div id="div01">当前用户:{{ user }}</div>
<div id="div02">传递多个值:</div>
</body>
<body>
<div id="title">模板语言变量传值</div>
<div id="div01">传递多个值:
学号:{{ sno }}
姓名:{{ name }}
性别:{{ gender }}
年龄:{{ birthday }}
</div>
</body>
<body>
<div id="title">模板语言变量传值</div>
<div id="div01">传递多个值--- List集合:
学号:{{ student.0 }}
姓名:{{ student.1 }}
年龄:{{ student.2 }}
生日:{{ student.3 }}
</div>
</body>
<body>
<div id="title">模板语言变量传值</div>
<div id="div01">传递多个值--- dict集合:
学号:{{ student.sno}}
姓名:{{ student.name }}
年龄:{{ student.gender }}
生日:{{ student.birthday }}
</div>
</body>
<body>
<div id="title">模板语言变量传值</div>
<div id="div01">传递多个值--- 对象:
学号:{{ student.sno}}
姓名:{{ student.name }}
年龄:{{ student.gender }}
生日:{{ student.birthday }}
</div>
</body>
模版中的if标签和for 标签
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>youku首页</title>
<link type="text/css" rel="stylesheet" href="{% static 'css/basic.css' %}">
</head>
<body>
<div id="title">
<div>
<div id="left">优酷首页</div>
<div id="right">
{% if type == '1' %}
欢迎您!{{ user }}[普通会员]
{% elif type == '2' %}
欢迎您!{{ user }}[高级会员]
{% elif type == '3' %}
欢迎您!{{ user }}[管理员]
{% else %}
<a href = "{% url 'login' %}">登录</a>
{% endif %}
</div>
</div>
</div>
<div id="bigpic"><img src="{% static "img/index.png" %}"></div>
</body>
</html>
<body>
<div id="title">使用DataTable展示学员信息</div>
<div id="content">
<!--table class="table table-striped table-bordered table-hover"-->
<table class="table table-striped table-hover table-bordered" id="student">
<thead>
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>出生日期</th>
<th>手机号码</th>
<th>邮箱地址</th>
<th>家庭住址</th>
</tr>
</thead>
<tbody>
{% for student in students %}
<tr>
<td style="background-color: navy;color:#FFF">{{ forloop.counter }}</td>
<td>{{ student.sno }}</td>
<td>{{ student.name }}</td>
<td>{{ student.gender }}</td>
<td>{{ student.birthday }}</td>
<td>{{ student.mobile }}</td>
<td>{{ student.email }}</td>
<td>{{ student.address }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
常用过滤器
https://www.cnblogs.com/huangxm/p/6286144.html