Django - 如何使变量可用于所有模板? [英] Django - How to make a variable available to all templates?
问题描述
我想知道如何将变量传递给所有的模板,而不必在我的views.py文件中的每个方法上重复相同的代码。
在下面的示例中,我想使类别(类别对象数组)可用于网络应用程序中的所有模板。
例如:我想避免写类别:每个方法的类别。可能吗?
一种查看方法
$ b $
category = Category.objects.all()
如果is_logged_in(请求)为False:
返回render_to_response ('users / signup.html',{'is_logged_in':is_logged_in(request),'categories':categories},context_instance = RequestContext(request))
else:
return render_to_response('users / front_page .html',{'is_logged_in':is_logged_in(request),'categories':categories},context_instance = RequestContext(request))
另一种查看方法
def another_view_method(request):
类别= Category.objects.all()
返回render_to_response('eg / front_page.html',{'is_logged_in':is_logged_in(request),'categories':categories},context_instance = RequestContext(request))
你想要的是一个 TEMPLATE_CONTEXT_PROCESSORS
,它很容易创建一个例如假设你有一个名为 custom_app $的应用程序c $ c>你必须按照下面的步骤创建一个。
- 添加
custom_app
toINSTALLED_APPS
insettings.py
- 创建一个
context_processors.py
到custom_app
文件夹。 -
添加下一个代码
def categories_processor(request):
categories = Category.objects.all()
return {'categories' :类别}
-
添加
context_processors.py
toTEMPLATE_CONTEXT_PROCESSORS
insettings.py
TEMPLATE_CONTEXT_PROCESSORS + =(custom_app.context_processors.categories_processor,)
现在,您可以在al中使用 {{categories}}
l模板:D
从Django 1.8开始
添加 TEMPLATE_CONTEXT_PROCESSORS
,在设置中,您必须添加下一个代码:
TEMPLATES [ 0] ['OPTIONS'] ['context_processors']。append(custom_app.context_processors.categories_processor)
I would like to know how to pass a variable to all my templates, without repeating the same code on every method in my views.py file?
In the example below I would like to make categories (an array of category objects) available to all templates in the web app.
Eg: I would like to avoid writing 'categories':categories on every method. Is it possible?
One view method
def front_page(request):
categories = Category.objects.all()
if is_logged_in(request) is False:
return render_to_response('users/signup.html', {'is_logged_in': is_logged_in(request), 'categories':categories}, context_instance=RequestContext(request))
else:
return render_to_response('users/front_page.html', {'is_logged_in': is_logged_in(request), 'categories':categories},context_instance=RequestContext(request))
Another view method
def another_view_method(request):
categories = Category.objects.all()
return render_to_response('eg/front_page.html', {'is_logged_in': is_logged_in(request), 'categories':categories},context_instance=RequestContext(request))
What you want It's a TEMPLATE_CONTEXT_PROCESSORS
and it's very easy to create one for example assuming you have got an app named custom_app
you have to follow the next steps to create one.
- Add
custom_app
toINSTALLED_APPS
insettings.py
- Create a
context_processors.py
intocustom_app
folder. Add the next code
def categories_processor(request): categories = Category.objects.all() return {'categories': categories}
Add
context_processors.py
toTEMPLATE_CONTEXT_PROCESSORS
insettings.py
TEMPLATE_CONTEXT_PROCESSORS += ("custom_app.context_processors.categories_processor", )
And now you can use {{categories}}
in all the templates :D
As of Django 1.8
To add a TEMPLATE_CONTEXT_PROCESSORS
, in the settings you must add the next code:
TEMPLATES[0]['OPTIONS']['context_processors'].append("custom_app.context_processors.categories_processor")
这篇关于Django - 如何使变量可用于所有模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!