django之定义模板语法08({{ string | truncatewords:'2' }})

#index页面代码:
<html>
    <head>
        <title>DOcument</title>
    </head>
    <body>
        <h1>{{user}}</h1>
        </br>
        </br>
        <h1>过滤器</h1>
        <p>{{ string | truncatewords:‘2‘ }}</p>      //显示前2个字(非字母) 
    </body>
</html>
######views文件
# -*- coding: cp936 -*-
from django.http import HttpResponse
from django.template import loader,Context
import datetime

    #向html页面传递数据
    c=Context({‘string‘:‘my name is xiaodeng‘})   //显示结果为:  my name ...

    #渲染方法
    t=t.render(c)
    return HttpResponse(t)

django之定义模板语法08({{ string | truncatewords:'2' }})

时间: 2024-08-25 07:47:30

django之定义模板语法08({{ string | truncatewords:'2' }})的相关文章

django之定义模板语法02(for语句循环list数据)

1.定义index页面 <html> <head> <title>DOcument</title> </head> <body> <h1>{{user}}</h1> </br> </br> <h1>注意:book变量的模块变量格式不要漏掉了</h1> {% for book in user %} <li>{{book}}</li> //是{

django之定义模板语法03(for语句循环dict数据)

1.修改index页面 <html> <head> <title>DOcument</title> </head> <body> <h1>{{user}}</h1> </br> </br> <h1>注意:book变量的模块变量格式不要漏掉了</h1> {% for book in user %} <li>{{book}}</li> {% e

django之定义模板语法07(过滤器之时间操作)

1.index页面修改: <html> <head> <title>DOcument</title> </head> <body> <h1>{{user}}</h1> </br> </br> <h1>过滤器</h1> {% for book in user %} <li>{{book | upper |lower | capfirst}}</li

django之定义模板语法05(过滤器之upper)

//过滤器格式为:   变量 | upper(过滤器关键字) 1.index <html> <head> <title>DOcument</title> </head> <body> <h1>{{user}}</h1> </br> </br> <h1>过滤器</h1> {% for book in user reversed %} <li>{{book

django之定义模板语法06(过滤器之多重过滤)

<html> <head> <title>DOcument</title> </head> <body> <h1>{{user}}</h1> </br> </br> <h1>过滤器</h1> <p>1.运用到了Linux管道思想</p> <p>2.先将book的值变为大写,再转化为小写,再转化为首字母大写</p> {

django之定义模板语法04(for循环之关键字reversed)

1.index <html> <head> <title>DOcument</title> </head> <body> <h1>{{user}}</h1> </br> </br> <h1>reversed关键字</h1> {% for book in user reversed %} <li>{{book}}</li> {% endfor

Django框架之模板语法(重要!)

一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 1.模板语法之变量:语法为 {{ }}: 在 Django 模板中遍历复杂数据结构的关键是句点字符  .(也就是点) views.py def index(request): name = "hello haiyan" i = 200 l = [11,22,33,44,55] d = {"name":"haiyan","age

django之定义模板变量04(接收list数据)

<html> <head> <title>DOcument</title> </head> <body> <h1>{{user}}</h1> </br> <li>{{user.0}}</li> //注意:接收list数据的方法的特殊性 <li>{{user.1}}</li> <li>{{user.2}}</li> </br

django之定义模板变量03(第一个类)

1.修改index页面 <html> <head> <title>{{user}}</title> </head> <body> <h1>hello {{user}}</h1> </br> </br> <li>hello {{user.name}}</li> <li>hello {{user.age}}</li> <li>hel