• 模版语言 实现瀑布流页面


    模板

    1、模版的执行

    模版的创建过程,对于模版,其实就是读取模版(其中嵌套着模版标签),然后将 Model 中获取的数据插入到模版中,最后将信息返回给用户。

    def current_datetime(request):
        now = datetime.datetime.now()
        html = "<html><body>It is now %s.</body></html>" % now
        return HttpResponse(html)
    View Code
    from django import template
    t = template.Template('My name is {{ name }}.')
    c = template.Context({'name': 'Adrian'})
    print t.render(c)
    View Code
    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
    View Code
    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)
    View Code
    return render_to_response('Account/Login.html',data,context_instance=RequestContext(request))
    View Code

    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

    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  

    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     # student
    14     # studentDetail
    15     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; 980px;" class="clearfix">
    25         <div style=" 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=" 245px;float: left">
    33             {% for item in detail_list %}
    34                 {% detail1 item forloop.counter 4 2 %}
    35             {% endfor %}
    36         </div>
    37         <div style=" 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=" 245px;float: left">
    45              {% for item in detail_list %}
    46                  {% if forloop.counter|detail3:"4,0" %}
    47                     <div style=" 245px;">
    48                         <img style=" 245px;height: 200px;" src="/{{ 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_tag
    10 def my_simple_time(v1,v2,v3):
    11     return  v1 + v2 + v3
    12 
    13 @register.simple_tag
    14 def detail1(item,counter,allcount,remainder):
    15     temp = """
    16         <div style=" 245px;">
    17             <img style=" 245px;height: 200px;" src="/%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_tag
    33 def detail2(counter,allcount,remainder):
    34     """
    35     查看余数是否等于remainder
    36     :param counter:
    37     :param allcount:
    38     :param remainder:
    39     :return:
    40     """
    41     if counter%allcount == remainder:
    42         return True
    43     return False
    44 
    45 @register.filter
    46 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 True
    60     return False
  • 相关阅读:
    常见保护方式简介
    各种保护机制绕过手法
    ShellCode框架(Win32ASM编写)
    单例模式
    HTTP1.0、HTTP1.1、HTTP2.0的关系和区别
    java集合提供的排序算法
    Dubbox以及微服务
    进程栈帧
    java线程池
    Java多态的实现
  • 原文地址:https://www.cnblogs.com/shiluoliming/p/6693894.html
Copyright © 2020-2023  润新知