Pho*_*beB 5 django django-templates
我一定错过了设置自定义模板上下文的方法,因为它永远不会被调用.
在设置中:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django_authopenid.context_processors.authopenid",
"web.context_processors.my_hat",
)
Run Code Online (Sandbox Code Playgroud)
在web/context_processors.py中
from libs.utils import get_hat, get_project, my_hats
print 'heloooo'
def my_hat(request):
"""Insert some additional information into the template context
"""
import pdb
pdb.set_trace()
print 'hiiiiiiii'
return {'hat': get_hat(request),
'project': get_project(request),
}
Run Code Online (Sandbox Code Playgroud)
什么都没有输出,django进程查看和显示模板,而没有遇到这个.我错过了什么!?
感谢Insin,我错过了一些内容:
在view.py中
return render_to_response(template, {
'tasks': tasks,
},
context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
在模板中:
My current hat is {{hat}}
Run Code Online (Sandbox Code Playgroud)
Jon*_*nan 15
你还记得在渲染模板时使用RequestContext吗?
从Django 1.3开始,有一个新的快捷方式函数,默认情况下render使用RequestContext:
return render(request, template, {
'tasks': tasks,
})
Run Code Online (Sandbox Code Playgroud)