DIRS=[os.path.join(BASE_DIR,"templates")]
loader.get_template(template_name),返回一個Template對象
Template對象的render(RequestContext)方法,使用context渲染模板
from django.template import loader, RequestContext from django.http import HttpResponse def index(request): tem = loader.get_template('temtest/index.html') context = RequestContext(request, {}) return HttpResponse(tem.render(context))
from django.shortcuts import render def index(request): return render(request, 'temtest/index.html')
{{ variable }}
from django.db import models class HeroInfo(models.Model): ... def showName(self): return self.hname
from django.shortcuts import render from models import * def index(request): hero = HeroInfo(hname='abc') context = {'hero': hero} return render(request, 'temtest/detail.html', context)
{{hero.showName}}
{ %for ... in ...%} 循環邏輯 {{forloop.counter}}表示當前是第幾回循環 { %empty%} 給出的列表爲或列表不存在時,執行此處 { %endfor%}
{ %if ...%} 邏輯1 { %elif ...%} 邏輯2 { %else%} 邏輯3 { %endif%}
{ % comment % } 多行註釋 { % endcomment % }
{ %include "foo/bar.html" % }
{ % url 'name' p1 p2 %}
{ % csrf_token %}
if list1|length > 1
name|lower|upper
list|join:", "
value|default:"什麼也沒有"
value|date:'Y-m-d'
{#...#}
{# { % if foo % }bar{ % else % } #}
{ %block block_name%} 這裏能夠定義默認值 若是不定義默認值,則表示空字符串 { %endblock%}
{ % extends "base.html" %}
{ %block block_name%} 實際填充內容 { %endblock%}
{ % block block_name %} 區域內容 { % endblock block_name %}
<!DOCTYPE html> <html> <head> <title>{%block title%}{%endblock%} 水果超市</title> </head> <body> top--{{logo}} <hr/> {%block left%}{%endblock%} {%block content%}{%endblock%} <hr/> bottom </body> </html>
{%extends 'temtest/base.html'%} {%block title%}商品{%endblock%} {%block left%} <h1>goods left</h1> {%endblock%}
{%extends 'temtest/base.html'%} {%block title%}用戶中心{%endblock%} {%block left%} <font color='blue'>user left</font> {%endblock%}
{%extends 'temtest/base.html'%} {%block content%} 首頁內容 {%endblock content%}
{%extends 'temtest/base_goods.html'%} {%block content%} 商品正文列表 {%endblock content%}
{%extends 'temtest/base_user.html'%} {%block content%} 用戶密碼修改 {%endblock content%}
logo='welcome to itcast' def index(request): return render(request, 'temtest/index.html', {'logo': logo})
def goodslist(request): return render(request, 'temtest/goodslist.html', {'logo': logo})
def userpwd(request): return render(request, 'temtest/userpwd.html', {'logo': logo})
from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^list/$', views.goodslist, name='list'), url(r'^pwd/$', views.userpwd, name='pwd'), ]
視圖代碼: def index(request): return render(request, 'temtest/index2.html', { 't1': '<h1>hello</h1>' }) 模板代碼: {{t1}}
< 會轉換爲< > 會轉換爲> ' (單引號) 會轉換爲' " (雙引號)會轉換爲 " & 會轉換爲 &
{{t1|escape}}
{{ data|safe }}
{ % autoescape off %} {{ body }} { % endautoescape %}
{ { data|default:"<b>123</b>" }}
{ { data|default:"<b>123</b>" }}
def csrf1(request): return render(request,'booktest/csrf1.html') def csrf2(request): uname=request.POST['uname'] return render(request,'booktest/csrf2.html',{'uname':uname})
url(r'^csrf1/$', views.csrf1), url(r'^csrf2/$', views.csrf2),
<html> <head> <title>Title</title> </head> <body> <form method="post" action="/crsf2/"> <input name="uname"><br> <input type="submit" value="提交"/> </form> </body> </html>
<html> <head> <title>Title</title> </head> <body> {{ uname }} </body> </html>
<form> {% csrf_token %} ... </form>
from django.views.decorators.csrf import csrf_exempt @csrf_exempt def csrf2(request): uname=request.POST['uname'] return render(request,'booktest/csrf2.html',{'uname':uname})
<input type='hidden' name='csrfmiddlewaretoken' value='nGjAB3Md9ZSb4NmG1sXDolPmh3bR2g59' />