Django temple渲染

from django.template import Context, Template
t = Template('My name is {{ name }}.')
c = Context({'name': 'Stephane'})
t.render(c)

from django.template import Template, Context
raw_template = """

Dear {{ person_name }},


...
...

Thanks for placing an order from {{ company }}. It's scheduled to
... ship on {{ ship_date|date:"F j, Y" }}.


...
... {% if ordered_warranty %}
...

Your warranty information will be included in the packaging.


... {% else %}
...

You didn't order a warranty, so you're on your own when
... the products inevitably stop working.


... {% endif %}
...
...

Sincerely,
{{ company }}

"""
t = Template(raw_template)
import datetime
c = Context({'person_name': 'John Smith',
... 'company': 'Outdoor Equipment',
... 'ship_date': datetime.date(2009, 4, 2),
... 'ordered_warranty': False})
t.render(c)
u"

Dear John Smith,

\n\n

Thanks for placing an order from Outdoor
Equipment. It's scheduled to\nship on April 2, 2009.

\n\n\n

You
didn't order a warranty, so you're on your own when\nthe products
inevitably stop working.

\n\n\n

Sincerely,
Outdoor Equipment

"

字典渲染

from django.template import Template, Context
person = {'name': 'Sally', 'age': '43'}
t = Template('{{ person.name }} is {{ person.age }} years old.')
c = Context({'person': person})
t.render(c)
u'Sally is 43 years old.'

日期渲染

from django.template import Template, Context
import datetime
d = datetime.date(1993, 5, 2)
d.year
Dear {{ person_name }},


...
...

Thanks for placing an order from {{ company }}. It's scheduled to
... ship on {{ ship_date|date:"F j, Y" }}.


...
... {% if ordered_warranty %}
...

Your warranty information will be included in the packaging.


... {% else %}
...

You didn't order a warranty, so you're on your own when
... the products inevitably stop working.


... {% endif %}
...
...

Sincerely,
{{ company }}

"""
>>> t = Template(raw_template)
>>> import datetime
>>> c = Context({'person_name': 'John Smith',
... 'company': 'Outdoor Equipment',
... 'ship_date': datetime.date(2009, 4, 2),
... 'ordered_warranty': False})
>>> t.render(c)
u"

Dear John Smith,

\n\n

Thanks for placing an order from Outdoor
Equipment. It's scheduled to\nship on April 2, 2009.

\n\n\n

You
didn't order a warranty, so you're on your own when\nthe products
inevitably stop working.

\n\n\n

Sincerely,
Outdoor Equipment

"

字典渲染
>>> from django.template import Template, Context
>>> person = {'name': 'Sally', 'age': '43'}
>>> t = Template('{{ person.name }} is {{ person.age }} years old.')
>>> c = Context({'person': person})
>>> t.render(c)
u'Sally is 43 years old.'

日期渲染
>>> from django.template import Template, Context
>>> import datetime
>>> d = datetime.date(1993, 5, 2)
>>> d.year
1993
d.month
5
d.day
2
t = Template('The month is {{ date.month }} and the year is {{ date.year }}.')
c = Context({'date': d})
t.render(c)
u'The month is 5 and the year is 1993.'

类属性渲染

from django.template import Template, Context
class Person(object):
... def init(self, first_name, last_name):
... self.first_name, self.last_name = first_name, last_name
t = Template('Hello, {{ person.first_name }} {{ person.last_name }}.')
c = Context({'person': Person('John', 'Smith')})
t.render(c)
u'Hello, John Smith.'

数组、列表渲染

from django.template import Template, Context
t = Template('Item 2 is {{ items.2 }}.')
c = Context({'items': ['apples', 'bananas', 'carrots']})
t.render(c)
u'Item 2 is carrots.'

方法调用,不加括号

from django.template import Template, Context
person = {'name': 'Sally', 'age': '43'}
t = Template('{{ person.name.upper }} is {{ person.age }} years old.')
c = Context({'person': person})
t.render(c)
u'SALLY is 43 years old.'

属性

from django.template import Context
c = Context({"foo": "bar"})
c['foo']
'bar'
del c['foo']
c['foo']
Traceback (most recent call last):
...
KeyError: 'foo'
c['newvariable'] = 'hello'
c['newvariable']
'hello'

判断语句
{% if today_is_weekend %}

Welcome to the weekend!


{% endif %}

{% if today_is_weekend %}

Welcome to the weekend!


{% else %}

Get back to work.


{% endif %}

{% if athlete_list and coach_list %}
Both athletes and coaches are available.
{% endif %}

{% if not athlete_list %}
There are no athletes.
{% endif %}

{% if athlete_list or coach_list %}
There are some athletes or some coaches.
{% endif %}

{% if not athlete_list or coach_list %}
There are no athletes or there are some coaches.
{% endif %}

{% if athlete_list and not coach_list %}
There are some athletes and absolutely no coaches.
{% endif %}

多条件
{% if athlete_list %}
{% if coach_list or cheerleader_list %}
We have athletes, and either coaches or cheerleaders!
{% endif %}
{% endif %}

for语句
{% for athlete in athlete_list %}

{{ athlete.name }}



    {% for sport in athlete.sports_played %}
  • {{ sport }}

  • {% endfor %}

{% endfor %}

典型table
{% for country in countries %}


{% for city in country.city_list %}





{% endfor %}
Country #{{ forloop.parentloop.counter }} City #{{ forloop.counter }} {{ city }}

{% endfor %}

变量比较
{% ifequal user currentuser %}

Welcome!


{% endifequal %}

{% ifequal section 'sitenews' %}

Site News


{% endifequal %}

{% ifequal section "community" %}

Community


{% endifequal %}

注释
{# This is a comment #}

{% comment %}
This is a
multi-line comment.
{% endcomment %}

过滤
{{ name|lower }}

{{ my_list|first|upper }}

获取前30个词
{{ bio|truncatewords:"30" }}

格式化日期
{{ pub_date|date:"F j, Y" }}

设置模板的路径:setting.py
TEMPLATE_DIRS = (
'/home/django/mysite/templates',
)

动态模板路径
import os.path

TEMPLATE_DIRS = (
os.path.join(os.path.dirname(file), 'templates').replace('\','/'),
)

get_template动态加载模板
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)

render_to_response:::::::
from django.shortcuts import render_to_response
import datetime

def current_datetime(request):
now = datetime.datetime.now()
return render_to_response('current_datetime.html', {'current_date': now})

内嵌模板:
{% include 'nav.html' %}
{% include "nav.html" %}

extends模板的用法



{% block title %}{% endblock %}


My helpful timestamp site


{% block content %}{% endblock %}
{% block footer %}



Thanks for visiting my site.


{% endblock %}

{% extends "base.html" %}

{% block title %}The current time{% endblock %}

{% block content %}

It is now {{ current_date }}.


{% endblock %}

{% extends "base.html" %}

{% block title %}Future time{% endblock %}

{% block content %}

In {{ hour_offset }} hour(s), it will be {{ next_time }}.


{% endblock %}

你可能感兴趣的:(Django temple渲染)