变量的过滤器(filter)的使用

过滤器:upper, lower, first, capfirst

在 views.py 中修改

from django.shortcuts import render

# Create your views here.

def index(req):
s="hello" return render(req,"index.html",{"obj":s })

在 index.html中修改

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Template</h1> {{ obj|upper }}
{{ obj|lower }}
{{ obj|first }}
{{ obj|capfirst }}
</body>
</html>

过滤器:add

在 views.py 中修改

from django.shortcuts import render
# Create your views here. def index(req):
s6=1
return render(req,"index.html",{"obj":s6 })

在 index.html中修改

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Template</h1> {{ obj|add:5 }}
</body>
</html>

过滤器: default

如果值是False,就替换成设置的默认值,否则就是用本来的值

在 views.py 中修改

from django.shortcuts import render
import datetime # Create your views here. def index(req): ...
s7=[] return render(req,"index.html",{"obj":s7 })

在 index.html中修改

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Template</h1> {{ obj|default:"空" }}
</body>
</html>

过滤器: autoescape

在 views.py 中修改

from django.shortcuts import render
import datetime # Create your views here. def index(req):
...
s8="<a href='#'>跳转</a>"
return render(req,"index.html",{"obj":s8 })

在 index.html中修改

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Template</h1> {% autoescape off%}
{{ obj }}
{% endautoescape %}
</body>
</html>

或者

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Template</h1>
{{ obj | safe}}
</body>
</html>

csrf_token标签

用于生成csrf_token的标签,用于防治跨站攻击验证。注意如果你在view的index里用的是render_to_response方法,不会生效其实,这里是会生成一个input标签,和其他表单标签一起提交给后台的。

在 urls.py 中修改


from django.contrib import admin
from django.urls import path
from app01 import views urlpatterns = [
....
path('login/', views.login),
]

在 views.py 中修改

....
def login(req):
if req.method=="POST":
return HttpResponse("ok") return render(req, "login.html")

添加一个login.html 页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/login/" method="post">
<input type="text" name="user">
<input type="text" name="pwd">
<input type="submit" value="submit">
{% csrf_token %}
</form> </body>
</html>

Django 模版语法 二的更多相关文章

  1. django 模版 语法与使用

    目录 django 模版语法与使用 django模板语言介绍 (摘自官方文档) 链接 什么是模板? 模板语句的 注释 变量 {{ 变量 }} 点(.)在模板语言中有特殊的含义,用来获取对象的相应属性值 ...

  2. Django 模版语法与使用

    目录 Django 模版语法与使用 django模板语言介绍 (摘自官方文档) 链接 什么是模板? 模板语句的 注释 变量 {{ 变量 }} 点(.)在模板语言中有特殊的含义,用来获取对象的相应属性值 ...

  3. Django 模版语法 测试环境 ORM单表查询

    模版语法 传值 视图函数向前端html页面传值,基本上所有的数据类型都可以渲染在前端页面上. views.py from django.shortcuts import render, redirec ...

  4. Django 模版语法

    一.简介 模版是纯文本文件.它可以产生任何基于文本的的格式(HTML,XML,CSV等等). 模版包括在使用时会被值替换掉的 变量,和控制模版逻辑的 标签. {% extends "base ...

  5. django 模版语法及使用

    模版的定义 模版是一个文本,用语分离文档的表现形式和内容,通常用于生成html 模版当中能够使用的python语法非常少,for ,if 之类,还有ifequal,结束的时候也要写endifequal ...

  6. Django 模版语法 三

    使用自定义simple_tag 在 app01 下面创建 templatetags 文件夹,在创建 my_tag.py 文件,内容如下: from django import template fro ...

  7. Django 模版语法 一

    创建项目 django_template 和 app django-admin startproject django_template python manage.py startapp app01 ...

  8. Django 模板 语法 变量 过滤器 模板继承 组件 自定义标签和过滤器 静态文件相关

    本节目录 一 语法 二 变量 三 过滤器 四 标签Tags 五 模板继承 六 组件 七 自定义标签和过滤器 八 静态文件相关 一 语法   模板渲染的官方文档 关于模板渲染你只需要记两种特殊符号(语法 ...

  9. Django系列(二):Django的路由层,视图层和模板层

    1.Django的路由层 URL配置(URLconf)就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:我们就是以这种方式告诉Django,对于客户端发来的某 ...

随机推荐

  1. [Usaco2005 Feb]Feed Accounting 饲料计算

    Description Farmer John is trying to figure out when his last shipment of feed arrived. Starting wit ...

  2. hdu 1025 Constructing Roads In JGShining's Kingdom

    本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来 ...

  3. Latex新人教程

    1.LaTeX软件的安装和使用 方法A(自助):在MikTeX的官网下载免费的MikTeX编译包并安装.下载WinEdt(收费)或TexMaker(免费)等编辑界面软件并安装. 方法B(打包):在ct ...

  4. bzoj 1858: [Scoi2010]序列操作 || 洛谷 P2572

    记一下:线段树占空间是$2^{ceil(log2(n))+1}$ 这个就是一个线段树区间操作题,各种标记的设置.转移都很明确,只要熟悉这类题应该说是没有什么难度的. 由于对某区间set之后该区间原先待 ...

  5. ACM牛人博客

    ACM牛人博客 kuangbin kuangbin(新) wuyiqi wuyiqi(新) ACM!荣耀之路! 九野的博客 传说中的ACM大牛!!! read more

  6. 题解报告:hdu1995汉诺塔V(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1995 Problem Description 用1,2,...,n表示n个盘子,称为1号盘,2号盘,. ...

  7. 题解报告:hdu 2030 汉字统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2030 Problem Description 统计给定文本文件中汉字的个数. Input 输入文件首先 ...

  8. synchronized(3)修饰语句块之:synchronized(一般对象)

    synchronized(一般对象) 一次只有一个线程进入该代码块.此时,线程获得的是成员锁.例如: public class Thread7 { private Object xlock = new ...

  9. Codeforces Beta Round #96 (Div. 2) (A-E)

    写份DIV2的完整题解 A 判断下HQ9有没有出现过 #include <iostream> #include<cstdio> #include<cstring> ...

  10. echart分组柱形图绑定数据

    <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts </ ...