溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

94django_template

發(fā)布時(shí)間:2020-07-23 18:04:32 來源:網(wǎng)絡(luò) 閱讀:202 作者:chaijowin 欄目:編程語言

?

?

目錄

template... 1

設(shè)置template引擎:... 1

設(shè)置公用templates和公用static... 2

view中使用template... 4

去掉urlstatic硬編碼:... 5

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

內(nèi)置built-in template tagsfilters... 7

自定義template filter... 7

模板擴(kuò)展extendsinclude包含:... 8

?

?

?

template

?

django內(nèi)置了自己的模板引擎,和jinja很像,使用簡(jiǎn)單;

?

django默認(rèn)會(huì)在app_name/templates/下尋找模板,在app_name/templates/下再建立app_name,這樣app_name/templates/app_name/下存放與該app相關(guān)的模板,因?yàn)槟J(rèn)django會(huì)去所有的app下找模板,可能會(huì)優(yōu)先找到其它app下的模板;

?

默認(rèn)會(huì)到app_name/static/下尋找靜態(tài)文件;

?

?

設(shè)置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.

?

?

設(shè)置公用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 }}

?

模板引擎支持循環(huán)、判斷、過濾器:

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 %}

<li> {{ person.name }} </li>

{% endfor %}

?

if

{% if max > 10 %}

<li>max value is {{ max }} </li>

{% else %}

<li>max value is 10 </li>

{% 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、attrmethod、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

<img src="/static/django.png">?? #mysite/polls/static/django.png

{% if latest_question_list %}

??? <ul>

??????? {% for question in latest_question_list %}

??????? <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

??????? {% endfor %}

??? </ul>

{% endif %}

?

?

去掉urlstatic硬編碼:

?

mysite/polls/templates/polls/index.html

{% load static %}

?

<img src="{% static 'django.png' %}">

{% if latest_question_list %}

??? <ul>

??????? {% for question in latest_question_list %}

??????? <!--<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>-->

??????? <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

??????? {% endfor %}

??? </ul>

{% endif %}

?

注:

{% url 'detail' question.id %}?? #appurls.py中定義的name有關(guān);app namespace改后,'detail'改為'polls:detail'

?

?

獲取和渲染模板:

?

1

from django.template.loader import get_template

def get_template(template_name, using=None):?? #返回一個(gè)模板對(duì)象,再使用模板對(duì)象的類方法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)

?

?

內(nèi)置built-in template tagsfilters

?

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

csrf_token?? #htmlform標(biāo)簽中使用,{% csrf_tokne %}

extends?? #模板擴(kuò)展,{% extends 'base.html' %}

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

url?? # <h4><a href="{% url 'article_detail' article.id %}">{{ article.title }}</a></h4>

filter

for

for ... empty

if

?

例:

{% for ad in ads %}

<a href="{{ ad.callback_url }}" target="_blank">

??? <img id="slide-img-{{ forloop.counter }}" src="{{ MEDIA_URL }}{{ ad.image_url }}" alt="" />

</a>

{% endfor %}

?

?

過濾器使用格式:

{{ 變量|過濾器:參數(shù) }}

add?? # <div class="module1_{{ forloop.counter|add:2 }} box">

center

date?? # <div>{{ article.date_publish|date:'d' }}</div>{{ article.date_publish|date:'Y-m-d' }}

default

dictsort

dictsortreversed

filesizeformat

length

last

ljust

lower

random

rjust

safe?? # 告訴browser,這個(gè)變量是安全的,可以渲染,沒必要顯示純文本,{{ article.content | safe }}

title

truncatewords?? #如果后端傳的數(shù)據(jù)太多,只顯示一部分,{{ value|truncatewords:3 }},截取3個(gè)單詞

?

?

i18n

tz

?

static?? #{% load static %}{% load staticfiles %}

get_static_prefix?? # <img src="{% get_static_prefix %}images/hi.jpg" alt="Hi!">

get_media_prefix

?

例:

{% extends "base.html" %}

{% block content %}

???????? <h3>Publishers</h3>

???????? <ul>

?????????????????? {% for publisher in object_list %}

?????????????????? <li>{{ publisher.name }}</li>

?????????????????? {% endfor %}

???????? </ul>

{% 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 %}

??? <dl>

??????? <dt class="num fl">{{ forloop.counter }}</dt>?? #The current iteration of the loop (1-indexed)

??????? <dd>

?? ?????????<a href="/company/2/"><h2>{{ current_org }}</h2></a>

??????????? <p>{{ current_org.address }}</p>

??????? </dd>

??? </dl>

{% 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 %}

?

<img src="{% static 'django.png' %}">

{% if latest_question_list %}

??? <ul>

??????? {% for question in latest_question_list %}

??????? <!--<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>-->

??????? <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a>

??????? -- {{ question|question_choice_count }}</li>

??????? {% endfor %}

??? </ul>

{% endif %}

?

?

模板擴(kuò)展extendsinclude包含:

?

實(shí)現(xiàn)網(wǎng)頁布局;

?

extends是繼承模板,然后自定義可設(shè)置的block;

include是導(dǎo)入一個(gè)模板片段到該位置;

?

?

?


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI