模板传参
1.在views.py中写入
def index_handler(request):
context = {
'username':'name1',
'password':'pass1'
}
return render(request,'index.html',context)
在template目录中创建index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello templates
<br/>
用户名:{
{
username }}
<br/>
密码:{
{
password }}
</body>
</html>
模板基础语法
未创建项目请参考本人的另一篇博客
1.参数输出
context = {
"key1":"value1",
"key2":["value1","value2"],
"key3":{
"key3_1":"value3_1",
"key3_2":"value3_2",
} }
注:
变量名不能以下划线开头
{
# 1.普通值获取 #}
{
{
key1 }}
<br/>
{
# 2.列表值获取 #}
{
{
key2.0}} {
{
key2.1 }}
<br/>
{
# 3.字典值获取 #}
{
{
key3.key3_1 }} {
{
key3.key3_2 }}
注:
{
# ... #}是模板语法中的注释
2.循环语句
context = {
"hobbys":["Python","Java","C"]
}
{
% for hobby in hobbys %}
爱好:{
{
hobby }}<br/>
{
% endfor %}
注:
{
{
用于输出变量 }}
{
% 命令语句 %}
3.判断语句
def t3_handler(request):
context = {
'message':'这是一个提示信息,根据style来决定颜色',
'style':