94django_template
admin
2023-07-08 12:23:44
0次

 

 

目录

template:... 1

设置template引擎:... 1

设置公用templates和公用static:... 2

view中使用template:... 4

去掉url和static硬编码:... 5

获取和渲染模板:... 5

内置built-in template tags和filters:... 7

自定义template filter:... 7

模板扩展extends和include包含:... 8

 

 

 

template:

 

django内置了自己的模板引擎,和jinja很像,使用简单;

 

django默认会在app_name/templates/下寻找模板,在app_name/templates/下再建立app_name,这样app_name/templates/app_name/下存放与该app相关的模板,因为默认django会去所有的app下找模板,可能会优先找到其它app下的模板;

 

默认会到app_name/static/下寻找静态文件;

 

 

设置template引擎:

 

TEMPLATES = [

    {

        'BACKEND': 'django.template.backends.django.DjangoTemplates',

        # 'BACKEND': 'django.template.backends.jinja2.Jinja2',

        'DIRS': [os.path.join(BASE_DIR, 'templates')],

        'APP_DIRS': True,

        'OPTIONS': {

            'context_processors': [

                'django.template.context_processors.debug',

                'django.template.context_processors.request',

                'django.contrib.auth.context_processors.auth',

                'django.contrib.messages.context_processors.messages',

            ],

        },

    },

]

 

context_processors are functions that receive the current HttpRequest as an argument and return a dict of data to be added to the rendering context.

 

 

设置公用templates和公用static:

mysite/mysite/settings.py

TEMPLATES = [

    {

        'BACKEND': 'django.template.backends.django.DjangoTemplates',

        'DIRS': [os.path.join(BASE_DIR, 'templates')],

        'APP_DIRS': True,

……

STATIC_URL = '/static/'

STATICFILES_DIRS = [

    os.path.join(BASE_DIR, 'static'),

]

 

 

variables:

{{ var }}、{{ dict.key }}、{{ var.attr }}、{{ var.method }}、{{ var.index }}

 

模板引擎支持循环、判断、过滤器:

filter:

{{ list|join:"," }}、{{ name|lower }}

{{ now | date:"Y-m-d" }}

{{ name | length }}

 

tags:

{% tag xxx %}{% endtag %}

{% for ... %}{% endfor %}

{# comment #}

 

for:

{% for person in person_list %}

  • {{ person.name }}
  • {% endfor %}

     

    if:

    {% if max > 10 %}

  • max value is {{ max }}
  • {% else %}

  • max value is 10
  • {% endif %}

     

    例:

    >>> from django.template import Template,Context

    >>> t = Template('My name is {{ name }}')

    >>> c = Context({'name':'jowin'})   #context可以是 dict、属性、方法、tuple|list

    >>> t.render(c)

    'My name is jowin'

     

    >>> t = Template('my name is {{ user.name }}')   #变量查找,dict、attr、method、list、tuple

    >>> class Person:

    ...     def __init__(self,name):

    ...             self.name = name

    ...

    >>> user = Person('jowin')

    >>> user.name

    'jowin'

    >>> c = Context({'user':user})

    >>> t.render(c)

    'my name is jowin'

     

    >>> t = Template('my name is {{ user.name }}')

    >>> class Person:

    ...     def name(self):

    ...             return 'jowin'

    ...

    >>> user = Person()

    >>> user.name()

    'jowin'

    >>> c = Context({'user':user})

    >>> t.render(c)

    'my name is jowin'

     

     

    view中使用template:

     

    render源码,from django.shortcuts import render:

    def render(request, template_name, context=None, content_type=None, status=None, using=None):

        """

        Returns a HttpResponse whose content is filled with the result of calling

        django.template.loader.render_to_string() with the passed arguments.

        """

        content = loader.render_to_string(template_name, context, request, using=using)

        return HttpResponse(content, content_type, status)

     

    另,render_to_response(),老版本1.6用;

     

     

    mysite/polls/views.py

    from django.http import HttpResponse

    from .models import Question

    from django.template import loader

     

    # def index(request):

    #     latest_question_list = Question.objects.order_by('-pub_date')[:4]

    #     template = loader.get_template('polls/index.html')

    #     context = {'latest_question_list': latest_question_list}

    #     # output = ', '.join([q.question_text for q in latest_question_list])

    #     return HttpResponse(template.render(context))   #方1

    def index(request):

        latest_question_list = Question.objects.order_by('-pub_date')[:4]

        context = {'latest_question_list': latest_question_list}

        return render(request, 'polls/index.html', context)   #方2

     

    mysite/polls/templates/polls/index.html

       #mysite/polls/static/django.png

    {% if latest_question_list %}

       

    {% endif %}

     

     

    去掉url和static硬编码:

     

    mysite/polls/templates/polls/index.html

    {% load static %}

     

    {% if latest_question_list %}

       

              {% for question in latest_question_list %}

             

             

    • {{ question.question_text }}
    •         {% endfor %}

         

    {% endif %}

     

    注:

    {% url 'detail' question.id %}   #app的urls.py中定义的name有关;app namespace改后,'detail'改为'polls:detail';

     

     

    获取和渲染模板:

     

    方1:

    from django.template.loader import get_template

    def get_template(template_name, using=None):   #返回一个模板对象,再使用模板对象的类方法Template中的render()渲染

        """

        Loads and returns a template for the given name.

        Raises TemplateDoesNotExist if no such template exists.

        """

        chain = []

        engines = _engine_list(using)

        for engine in engines:

            try:

                return engine.get_template(template_name)

            except TemplateDoesNotExist as e:

                chain.append(e)

     

        raise TemplateDoesNotExist(template_name, chain=chain)

     

    class Template(object):

        def __init__(self, template_string, origin=None, name=None, engine=None):

                       ……

        def _render(self, context):

            return self.nodelist.render(context)

     

        def render(self, context):

            "Display stage -- can be called many times"

            with context.render_context.push_state(self):

                if context.template is None:

                    with context.bind_template(self):

                        context.template_name = self.name

                        return self._render(context)

                else:

                    return self._render(context)

     

     

    方2:

    from django.shortcuts import render

    def render(request, template_name, context=None, content_type=None, status=None, using=None):

        """

        Returns a HttpResponse whose content is filled with the result of calling

        django.template.loader.render_to_string() with the passed arguments.

        """

        content = loader.render_to_string(template_name, context, request, using=using)

        return HttpResponse(content, content_type, status)

     

     

    方3:

    from django.template.loader import render_to_string

    def render_to_string(template_name, context=None, request=None, using=None):

        """

        Loads a template and renders it with a context. Returns a string.

     

        template_name may be a string or a list of strings.

        """

        if isinstance(template_name, (list, tuple)):

            template = select_template(template_name, using=using)

        else:

            template = get_template(template_name, using=using)

        return template.render(context, request)

     

     

    内置built-in template tags和filters:

     

    block   #Defines a block that can be overridden by child templates.,{% block custom_title %}首页{% endblock %}

    csrf_token   #html的form标签中使用,{% csrf_tokne %}

    extends   #模板扩展,{% extends 'base.html' %}

    include   #模板包含,{% include 'pagination.html' %}

    url   #

    {{ article.title }}

    filter

    for

    for ... empty

    if

     

    例:

    {% for ad in ads %}

       

    {% endfor %}

     

     

    过滤器使用格式:

    {{ 变量|过滤器:参数 }}

    add   #

    center

    date   #

    {{ article.date_publish|date:'d' }}
    ,{{ article.date_publish|date:'Y-m-d' }}

    default

    dictsort

    dictsortreversed

    filesizeformat

    length

    last

    ljust

    lower

    random

    rjust

    safe   # 告诉browser,这个变量是安全的,可以渲染,没必要显示纯文本,{{ article.content | safe }}

    title

    truncatewords   #如果后端传的数据太多,只显示一部分,{{ value|truncatewords:3 }},截取3个单词

     

     

    i18n

    tz

     

    static   #{% load static %}吗{% load staticfiles %}

    get_static_prefix   # Hi!

    get_media_prefix

     

    例:

    {% extends "base.html" %}

    {% block content %}

            

    Publishers

            

                         {% for publisher in object_list %}

                        

    • {{ publisher.name }}
    •                    {% endfor %}

              

    {% endblock %}

     

    {% ifequal a b %} ... {% endifequal %} is an obsolete way to write {% if a == b %} ... {% endif %}

    Likewise,

    {% ifnotequal a b %} ... {% endifnotequal %} is superseded by {% if a != b %} ... {% endif %}

    The ifequal and ifnotequal tags will be deprecated in a future release.

     

    {% for current_org in hot_orgs %}

       

           

    {{ forloop.counter }}
       #The current iteration of the loop (1-indexed)

           

                

    {{ current_org }}

               

    {{ current_org.address }}

           

       

    {% endfor%}

     

    自定义template filter:

     

    django寻找自定义filter的目录是app_name/templatetags/;

     

    例:

    mysite/polls/templatetags/mytags.py

    from django import template

     

    register = template.Library()

     

    @register.filter

    def lower(text):

        return text.lower()

     

    @register.filter

    def question_choice_count(question):

        return question.choice_set.count()

     

    @register.filter

    def question_choice_count_add(question, num):

        return question.choice_set.count() + int(num)

     

    mysite/polls/templates/polls/index.html

    {% load static %}

    {% load mytags %}

     

    {% if latest_question_list %}

       

              {% for question in latest_question_list %}

             

             

    • {{ question.question_text }}

              -- {{ question|question_choice_count }}

    •         {% endfor %}

         

    {% endif %}

     

     

    模板扩展extends和include包含:

     

    实现网页布局;

     

    extends是继承模板,然后自定义可设置的block;

    include是导入一个模板片段到该位置;

     

     

     


    上一篇:96django_modelform

    下一篇:92django_url

    相关内容

    热门资讯

    空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
    空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
    格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
    空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
    格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
    品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
    空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
    空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
    中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
    中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...