django 的模板语言
1、模版的执行
模版的创建过程,对于模版,其实就是读取模版(其中嵌套着模版标签),然后将 Model 中获取的数据插入到模版中,最后将信息返回给用户。
def current_datetime(request): now = datetime.datetime.now() html = "<html><body>It is now %s.</body></html>" % now return HttpResponse(html)
from django import template t = template.Template('My name is {{ name }}.') c = template.Context({'name': 'Adrian'}) print t.render(c)
import datetime from django import template import DjangoDemo.settings now = datetime.datetime.now() fp = open(settings.BASE_DIR+'/templates/Home/Index.html') t = template.Template(fp.read()) fp.close() html = t.render(template.Context({'current_date': now})) return HttpResponse(html
from django.template.loader import get_template from django.template import Context from django.http import HttpResponse import datetime def current_datetime(request): now = datetime.datetime.now() t = get_template('current_datetime.html') html = t.render(Context({'current_date': now})) return HttpResponse(html)
return render_to_response('Account/Login.html',data,context_instance=RequestContext(request))
我是这么写的
2、模版语言
模板中也有自己的语言,该语言可以实现数据展示
- {{ item }}
- {% for item in item_list %} <a>{{ item }}</a> {% endfor %}
forloop.counter
forloop.first
forloop.last - {% if ordered_warranty %} {% else %} {% endif %}
- 母板:{% block title %}{% endblock %}
子板:{% extends "base.html" %}
{% block title %}{% endblock %} - 帮助方法:
{{ item.event_start|date:"Y-m-d H:i:s"}}
{{ bio|truncatewords:"30" }}
{{ my_list|first|upper }}
{{ name|lower }}
if方法
for循环
for循环索引
反向操作
{{% empty %}}等价于做了if判断,如果为空
第一种方法:
第二种方法:
字典循环
需求,要求把输出的内容都变成大写
对时间的格式化
<li> today is {{today | date:"Y-m-d"}}
3、自定义simple_tag
a、在app中创建templatetags模块
视频里面这么干
b、创建任意 .py 文件,如:xx.py
#!/usr/bin/env python #coding:utf-8 from django import template from django.utils.safestring import mark_safe from django.template.base import resolve_variable, Node, TemplateSyntaxError register = template.Library() @register.simple_tag def my_simple_time(v1,v2,v3): return v1 + v2 + v3 @register.simple_tag def my_input(id,arg): result = "<input type='text' id='%s' class='%s' />" %(id,arg,) return mark_safe(result)
from django import template register = template.Library() def percent(value): return value + "%" register.filter(percent)
c、在使用自定义simple_tag的html文件中导入之前创建的 xx.py 文件名
{% load xx %}
d、使用simple_tag
{% my_simple_time 1 2 3%} {% my_input 'id_username' 'hide'%}
e、在settings中配置当前app,不然django无法找到自定义的simple_tag
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01', )
simple_tag和filter的案例
#!/usr/bin/env python # -*- coding:utf-8 -*- from django import template from django.utils.safestring import mark_safe from django.template.base import resolve_variable, Node, TemplateSyntaxError register = template.Library() @register.simple_tag def my_simple_time(v1,v2,v3): return v1 + v2 + v3 @register.simple_tag def detail1(item,counter,allcount,remainder): temp = """ <div style="width: 245px;"> <img style="width: 245px;height: 200px;" src="/%s"> <p>%s</p> <p>%s</p> <p>%s</p> </div> """ if counter%allcount == remainder: temp = temp %(item['student__pic'], item['student__name'], item['student__salary'], item['letter_of_thanks']) return mark_safe(temp) else: return "" @register.simple_tag def detail2(counter,allcount,remainder): """ 查看余数是否等于remainder :param counter: :param allcount: :param remainder: :return: """ if counter%allcount == remainder: return True return False
{% load xx %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> </div> <style> .clearfix:after{ content: '.'; visibility: hidden; height: 0; clear: both; display: block; } </style> <div style="margin: 0 auto;width: 980px;" class="clearfix"> <div style="width: 245px;float: left"> {% for item in detail_list %} {% detail1 item forloop.counter 4 1 %} {% endfor %} </div> <div style="width: 245px;float: left"> {% for item in detail_list %} {% detail1 item forloop.counter 4 2 %} {% endfor %} </div> <div style="width: 245px;float: left"> {% for item in detail_list %} {% detail1 item forloop.counter 4 3 %} {% endfor %}
---------------------------------
#!/usr/bin/env python # -*- coding:utf-8 -*- from django import template from django.utils.safestring import mark_safe from django.template.base import resolve_variable, Node, TemplateSyntaxError register = template.Library() @register.filter def detail3(value,arg): """ 查看余数是否等于remainder arg="1,2" :param counter: :param allcount: :param remainder: :return: """ allcount, remainder = arg.split(',') allcount = int(allcount) remainder = int(remainder) if value%allcount == remainder: return True return False
{% load xx %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> </div> <style> .clearfix:after{ content: '.'; visibility: hidden; height: 0; clear: both; display: block; } </style> <div style="margin: 0 auto;width: 980px;" class="clearfix"> <div style="width: 245px;float: left"> {% for item in detail_list %} {% if forloop.counter|detail3:"4,0" %} <div style="width: 245px;"> <img style="width: 245px;height: 200px;" src="/{{ item.student__pic }}"> <p>{{ item.student__name }}</p> <p>{{ item.student__salary }}</p> <p>{{ item.letter_of_thanks }}</p> </div> {% endif %} {% endfor %} </div> </div> </body> </html>
作者:沐禹辰
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。