首页 > 代码库 > 模版语言 实现瀑布流页面
模版语言 实现瀑布流页面
模板
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 templatet = template.Template(‘My name is {{ name }}.‘)c = template.Context({‘name‘: ‘Adrian‘})print t.render(c)
import datetimefrom django import templateimport 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_templatefrom django.template import Contextfrom django.http import HttpResponseimport 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 }}
3、自定义simple_tag
a、在app中创建templatetags模块
b、创建任意 .py 文件,如:xx.py
+ View Code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/usr/bin/env python #coding:utf-8 from django import template from django.utils.safestring import mark_safe 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) |
c、在使用自定义simple_tag的html文件中导入之前创建的 xx.py 文件名
?
1 | { % load xx % } |
d、使用simple_tag
?
1 2 | { % my_simple_time 1 2 3 % } { % my_input ‘id_username‘ ‘hide‘ % } |
e、在settings中配置当前app,不然django无法找到自定义的simple_tag
+ View Code?
1 2 3 4 5 6 7 8 9 | INSTALLED_APPS = ( ‘django.contrib.admin‘ , ‘django.contrib.auth‘ , ‘django.contrib.contenttypes‘ , ‘django.contrib.sessions‘ , ‘django.contrib.messages‘ , ‘django.contrib.staticfiles‘ , ‘app01‘ , ) |
更多见文档:https://docs.djangoproject.com/en/1.10/ref/templates/language/
views.py
1 import json 2 from django.shortcuts import render,HttpResponse 3 from app01 import models 4 # Create your views here. 5 def index(request): 6 # models.BxSlider.objects.filter(status=1) 对象 7 queryset_dict = models.BxSlider.objects.filter(status=1).values(‘img‘,‘href‘,‘name‘) 8 # queryset_list = models.BxSlider.objects.filter(status=1).values_list(‘img‘,‘href‘,‘name‘) 9 return render(request,‘index.html‘, {‘queryset_dict‘: queryset_dict})10 11 def student(request):12 13 # student14 # studentDetail15 detail_list = models.StudentDetail.objects.filter(student__status=1).values(‘letter_of_thanks‘,"student__name","student__salary",‘student__company‘, ‘student__pic‘)16 print(detail_list)17 return render(request,‘student.html‘, {‘detail_list‘: detail_list})
student.html
1 {% load xx %} 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <title>Title</title> 7 <style> 8 .clearfix:after{ 9 content: ‘.‘;10 visibility: hidden;11 height: 0;12 clear: both;13 display: block;14 }15 </style>16 </head>17 <body>18 19 <div>20 21 </div>22 23 24 <div style="margin: 0 auto;width: 980px;" class="clearfix">25 <div style="width: 245px;float: left">26 {% for item in detail_list %}27 28 {% detail1 item forloop.counter 4 1 %}29 30 {% endfor %}31 </div>32 <div style="width: 245px;float: left">33 {% for item in detail_list %}34 {% detail1 item forloop.counter 4 2 %}35 {% endfor %}36 </div>37 <div style="width: 245px;float: left">38 {% for item in detail_list %}39 {% detail1 item forloop.counter 4 3 %}40 {% endfor %}41 42 </div>43 {# {{ forloop.counter|detail3 }}#}44 <div style="width: 245px;float: left">45 {% for item in detail_list %}46 {% if forloop.counter|detail3:"4,0" %}47 <div style="width: 245px;">48 <img style="width: 245px;height: 200px;" src="http://www.mamicode.com/{{ item.student__pic }}">49 <p>{{ item.student__name }}</p>50 <p>{{ item.student__salary }}</p>51 <p>{{ item.letter_of_thanks }}</p>52 </div>53 {% endif %}54 55 {% endfor %}56 57 </div>58 </div>59 60 </body>61 </html>
xx.py
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 from django import template 4 from django.utils.safestring import mark_safe 5 from django.template.base import Node, TemplateSyntaxError 6 7 register = template.Library() 8 9 @register.simple_tag10 def my_simple_time(v1,v2,v3):11 return v1 + v2 + v312 13 @register.simple_tag14 def detail1(item,counter,allcount,remainder):15 temp = """16 <div style="width: 245px;">17 <img style="width: 245px;height: 200px;" src="http://www.mamicode.com/%s">18 <p>%s</p>19 <p>%s</p>20 <p>%s</p>21 </div>22 """23 if counter%allcount == remainder:24 temp = temp %(item[‘student__pic‘],25 item[‘student__name‘],26 item[‘student__salary‘],27 item[‘letter_of_thanks‘])28 return mark_safe(temp)29 else:30 return ""31 32 @register.simple_tag33 def detail2(counter,allcount,remainder):34 """35 查看余数是否等于remainder36 :param counter:37 :param allcount:38 :param remainder:39 :return:40 """41 if counter%allcount == remainder:42 return True43 return False44 45 @register.filter46 def detail3(value,arg):47 48 """49 查看余数是否等于remainder arg="1,2"50 :param counter:51 :param allcount:52 :param remainder:53 :return:54 """55 allcount, remainder = arg.split(‘,‘)56 allcount = int(allcount)57 remainder = int(remainder)58 if value%allcount == remainder:59 return True60 return False
模版语言 实现瀑布流页面
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。