Django—如何使变量对所有模板可用?

时间:2022-09-25 07:45:33

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?

我想知道如何将变量传递给所有模板,而不必在视图中的每个方法上重复相同的代码。py文件?

In the example below I would like to make categories (an array of category objects) available to all templates in the web app.

在下面的示例中,我想让web应用程序中的所有模板都可以使用类别(一个类别对象数组)。

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))

2 个解决方案

#1


77  

What you want is a context processor, and it's very easy to create one. Assuming you have an app named custom_app, follow the next steps:

您需要的是一个上下文处理器,创建一个上下文处理器非常容易。假设您有一个名为custom_app的应用程序,请执行以下步骤:

  • Add custom_app to INSTALLED_APPS in settings.py (you've done it already, right?);
  • 在设置中向INSTALLED_APPS添加custom_app。py(你已经做过了,对吧?)
  • Create a context_processors.py into custom_app folder;
  • 创建一个context_processors。py custom_app文件夹;
  • Add the following code to that new file:

    在新文件中添加以下代码:

    def categories_processor(request):
     categories = Category.objects.all()            
     return {'categories': categories}
    
  • Add context_processors.py to TEMPLATE_CONTEXT_PROCESSORS in settings.py

    添加context_processors。py到template_context_processor在settings.py中

    TEMPLATE_CONTEXT_PROCESSORS += ("custom_app.context_processors.categories_processor", )
    

And now you can use {{categories}} in all the templates :D

现在您可以在所有模板中使用{{{{{categories}:D

As of Django 1.8

Django的1.8

To add a TEMPLATE_CONTEXT_PROCESSORS, in the settings you must add the next code:

要添加template_context_processor,在设置中必须添加下一个代码:

TEMPLATES[0]['OPTIONS']['context_processors'].append("custom_app.context_processors.categories_processor")

Or include that string directly in the OPTIONS.context_processors key in your TEMPLATES setting.

或者直接在选项中包含该字符串。context_processor在模板设置中。

#2


0  

As seen in this example, post Django 1.3 you can simply use render instead of render_to_response which doesn't require you to explicitly pass the context processor.

如本例所示,在Django 1.3之后,您可以使用render而不是render_to_response,这并不需要显式地传递上下文处理器。

def another_view_method(request):
    categories = Category.objects.all()
    return render(
        'eg/front_page.html',
        {'is_logged_in': is_logged_in(request), 'categories':categories}
    )

#1


77  

What you want is a context processor, and it's very easy to create one. Assuming you have an app named custom_app, follow the next steps:

您需要的是一个上下文处理器,创建一个上下文处理器非常容易。假设您有一个名为custom_app的应用程序,请执行以下步骤:

  • Add custom_app to INSTALLED_APPS in settings.py (you've done it already, right?);
  • 在设置中向INSTALLED_APPS添加custom_app。py(你已经做过了,对吧?)
  • Create a context_processors.py into custom_app folder;
  • 创建一个context_processors。py custom_app文件夹;
  • Add the following code to that new file:

    在新文件中添加以下代码:

    def categories_processor(request):
     categories = Category.objects.all()            
     return {'categories': categories}
    
  • Add context_processors.py to TEMPLATE_CONTEXT_PROCESSORS in settings.py

    添加context_processors。py到template_context_processor在settings.py中

    TEMPLATE_CONTEXT_PROCESSORS += ("custom_app.context_processors.categories_processor", )
    

And now you can use {{categories}} in all the templates :D

现在您可以在所有模板中使用{{{{{categories}:D

As of Django 1.8

Django的1.8

To add a TEMPLATE_CONTEXT_PROCESSORS, in the settings you must add the next code:

要添加template_context_processor,在设置中必须添加下一个代码:

TEMPLATES[0]['OPTIONS']['context_processors'].append("custom_app.context_processors.categories_processor")

Or include that string directly in the OPTIONS.context_processors key in your TEMPLATES setting.

或者直接在选项中包含该字符串。context_processor在模板设置中。

#2


0  

As seen in this example, post Django 1.3 you can simply use render instead of render_to_response which doesn't require you to explicitly pass the context processor.

如本例所示,在Django 1.3之后,您可以使用render而不是render_to_response,这并不需要显式地传递上下文处理器。

def another_view_method(request):
    categories = Category.objects.all()
    return render(
        'eg/front_page.html',
        {'is_logged_in': is_logged_in(request), 'categories':categories}
    )