python例子-Django项目中的模版特性

一、在网页上显示一个字符变量

  在views.py中定义处理函数:(记得在urls.py中绑定URL)

#coding: utf-8      #这里如果不指定utf-8的话,汉字输出就会报编码错误.
from django.shortcuts import render
 
def home(request):
    string = u"一个中文字符"
    return render(request, 'home.html', {'string': string})

  在templates模版文件夹下的模版文件中的格式:

{{ string }}

  注意:以后显示变量就使用{{ var }} 的形式.

二、for循环以及List,dict的显示

  在views.py中定义处理函数:(记得在urls.py中绑定URL)

#coding:utf-8
from django.shortcuts import render


# Create your views here.
def home(request):
        string = 'showString'
        list_arry = ['one','two','three','four','haha','It\'s gred']
        dict_info = {'site':u'网址','content':u'网页内容','name':''}return render(request,'home.html',{'str':string,'aList':list_arry,'dict_info':dict_info})

  模版文件中对应:

#list循环:
{% for i in aList %}      #循环
       {{ i }}    #输出循环变量
{% endfor %}     #结束循环

#dict循环
{% for key,value in dict_info.items %}
        {{ key }}:{{ value }}
{% empty %}
        <li>sorry it a empty</li> #如果在for循环中遇到Empty就使用{% empty %}进行判断;Empty:当list,dict为空,没有值时就时为Empty,可不是某个下标为空. 
{% endfor %}
#dict循环另一种方式
{{ dict_info.site }},{{dict_info.content}} #这里格式为 dict.key

三、for循环以及其中的常见判断

  在views.py中定义处理函数:(记得在urls.py中绑定URL)

#coding:utf-8
from django.shortcuts import render


# Create your views here.
def home(request):
        string = 'showString'
        list_arry = ['one','two','three','four','haha','It\'s gred']
        list_range = map(str,range(100))
        #empty_list = ['a','b','c','d',' ','f']
        empty_list = []
        return render(request,'home.html',{'str':string,'aList':list_arry,'list_range':list_range,'empty_list':empty_list})

  模版文件中对应:

#判断是否是list中最后一个
{% for num in list_range %}
        {{ num }}{% if not forloop.last %},{% endif %}
{% endfor %}

#判断list是否为空Empty
{% for i in empty_list%}
        {{ i }}
{% empty %}
        it's empty
{% endfor%}

  其他for中判断:

forloop.counter    索引从 1 开始算
forloop.counter0    索引从 0 开始算
forloop.revcounter    索引从最大长度到 1
forloop.revcounter0    索引从最大长度到 0
forloop.first    当遍历的元素为第一项时为真
forloop.last    当遍历的元素为最后一项时为真
forloop.parentloop    用在嵌套的 for 循环中,获取上一层 for 循环的 forloop

四、模版根据处理函数动态 生成连接:(可用于链接跳转)

  在views.py中定义处理函数:

#coding:utf-8
from django.shortcuts import render

def add(request,a,b):
        c = int(a) + int(b)
        return render(request,'add.html',{'jiafac':c})  #返回的都为同一个模版文件

def other(request,c,d):
        c = c + d
        return render(request,'add.html',{'otherc':c})  #返回的都为同一个模版文件

  在urls.py中绑定的URL:

urlpatterns = [
    url(r'^jiafa/(\d+)/(\d+)/$','learn.views.add',name='add'),
    url(r'^zifu/(\w+)/(\w+)/$','learn.views.other',name='other'),
    url(r'^admin/', include(admin.site.urls)),
]

  在模版中:(add.html)

{% url 'other' 'as' 'bd' as the_url %}    #格式为{% url '函数名' 参数1 参数2 ... %}  注意,如果参数为数字可以不用加引号,为字符需加引号,否则当做变量处理。如果该变量不存在就不会生成该链接,这里处理函数为other,参数为字符 'as' 'bd' 生成连接为 /zifu/as/bd ,客户端点击该链接就会执行相应的请求.  这里的url就会根据 urls.py中的正则定义带入参数生成链接。as为 取别名
<a href="{{ the_url }}"> linkto : {{ the_url }}</a>
</br>
c:{{ jiafac }}
</br>
otherc: {{ otherc }}

五、模版中的逻辑操作

  ==, !=, >=, <=, >, < 这些比较都可以在模板中使用

{% if var >= 90 %}
 great
{% elif var >= 80 %}
 good
{% elif var >= 70 %}
 ok
{% elif var >= 60 %}
 bad
{% else %}
 so bad
{% endif %}

  and, or, not, in, not in 也可以在模板中使用

{% if num <= 100 and num >= 0 %}
 num在0到100之间
{% else %}
 数值不在范围之内!
{% endif %}

  判断 'zifu' 在不在一个列表变量 List 中:

{% if 'zifu' in List %}
 字符在List中
{% else %}
 字符不在List中
{% endif %}

2015年10月22日00:39:05

 

posted @ 2015-10-22 00:40  超超xc  Views(334)  Comments(0Edit  收藏  举报
I suppose,were childrenonec.