94年django_template

模板

templatesstatic viewtemplate

urlstatic

内置模板tagsfilters

模板过滤器extendsinclude

模板

djangojinja

djangoapp_name/模板/app_name/模板/app_nameapp_name/模板/app_name appdjangoappapp

app_name/静态/

模板

模板=[

{

#“后端”:“django.template.backends.jinja2。Jinja2”

“DIRS”: [os.path。加入(BASE_DIR“模板”)],

“APP_DIRS”:真的,

“选项”:{

“context_processors”: [

django.template.context_processors.debug,

django.template.context_processors。请求”,

“django.contrib.auth.context_processors。身份验证”,

“django.contrib.messages.context_processors。消息”,

,

},

},

]

context_processors函数获得当前HttpRequest作为参数,添加到渲染上下文。

templatesstatic

mysite mysite/设置。py

模板=[

{

“后端”:“django.template.backends.django。DjangoTemplates”,

' APP_DIRS”:真的,

STATIC_URL='/静态的

变量:

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

过滤器

{{列表|加入:“,”}}{{名称|低}}

{{现在|日期:“Y-m-d}}

{{名称|长度}}

标记

{%标签xxx %}{%结束标记%}

{%……%}{% endfor %}

{#评论#}

{%人在person_list %}

<李> {{person.name}}

{% endfor %}

如果{%如果麦克斯> 10%}

<李> max值{{马克斯}}

{%其他%}

<李> max值是10

{% endif %}

> django。模板导入模板、上下文

> t=模板('我的名字叫{{名称}})

> c=上下文({“名称”:“jowin”}) #

> t.render (c)

“我的名字叫jowin”

> t=模板(“我的名字叫{{user.name}}”) # dictattrmethodlisttuple

>类人:

…def __init__(自我,名字):

…self.name=名字

>用户=人(jowin)

> user.name

“jowin”

> c=上下文({“用户”:用户})

> t.render (c)

“我的名字叫jowin”

> t=模板(“我的名字叫{{user.name}}”)

>类人:

…def名称(自我):

…返回“jowin”

>用户=人()

> user.name ()

“jowin”

> c=上下文({“用户”:用户})

> t.render (c)

“我的名字叫jowin”

viewtemplate

renderfrom django。快捷方式进口呈现

def渲染(content_type=None,状态=None,使用=None):

" "

返回HttpResponse的内容充满了调用的结果

django.template.loader.render_to_string()与传递的参数。"

"

内容=加载程序。render_to_string (template_name、上下文要求,使用=使用)

(内容、content_type状态)

render_to_response (1.6)

mysite/调查/观点。py

django。http进口HttpResponse

得进口问题

从django。模板导入装载机

# def指数(请求):

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

#模板=loader.get_template(民意调查/index . html)

#上下文={latest_question_list: latest_question_list}

# #输出=',' . join ([q。question_text latest_question_list问的)

#返回HttpResponse (template.render(上下文)# 1

def指数(请求):

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

背景={latest_question_list: latest_question_list}

返回渲染(请求,“民意调查/指数。html的,上下文)# 2

mysite/调查/模板/调查/索引。html

# mysite/调查/静态/django。png

{%如果latest_question_list %}

{% endif %}

urlstatic

mysite/调查/模板/调查/索引。html

{%加载静态%}

{%如果latest_question_list %}