如何将变量传递给django中的所有模板? [英] How do I pass variables to all templates in django?

查看:169
本文介绍了如何将变量传递给django中的所有模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将变量(浏览器变量)传递给我的应用程序中的所有模板。任何关于如何使其工作的建议?

I am trying to pass variables (browser variable) to all my templates in my app. Any advice on how to get it to work?

查看:

def browser(request):
    primary_cat_list = Categories.objects.order_by("category")
    subcat_list = SubCategories.objects.order_by("sub_category")
    product = Productbackup.objects.order_by("website")
    browser =  list(chain(primary_cat_list, subcat_list, product))
    return render_to_response('reserve/templates/base.html', locals(), context_instance=RequestContext(request))

模板:

{% for prod in browser %} {{ prod }}, {% endfor %}


推荐答案

你,我的朋友,在上下文处理器的市场上。

从< a href =http://blog.madpython.com/2010/04/07/django-context-processors-best-practice/>一个博客条目,由一个远的博客和博学的技术作家写的比我:

From a blog entry written by a far nimbler and erudite technical writer than I:


什么是模板上下文处理器?

Django的上下文处理器是一种允许您向模板提供数据和回调的工具。

Django’s context processors are a facility that allows you to provide data and callbacks to your templates.

您可以通过以下两种方式之一执行此操作:

You can do so in one of two ways:


  • 在个别请求的基础上:通过上下文值到您的 render_to_response()调用

  • 全局:创建一个上下文处理器方法,它接受一个 HttpRequest 对象作为输入,并返回有效负载或回调,然后
    注册您的 settings.py ,然后使用内置的 RequestContext 提供您的 render_to_response()属性
    而不是你自己的(你可以随时扩展 RequestContext 以在个别请求的基础上添加更多的数据。)

  • On an individual request basis: by passing a custom Context value to your render_to_response() call
  • Globally: by creating a context processor method that accepts a HttpRequest object as input, and returns a payload or callback, then registering the context processor in your settings.py, then providing your render_to_response() call with the built-in RequestContext attribute instead of your own (you can always extend RequestContext to add more data on an individual request basis of course).

如果将数据传递给模板的方法听起来很荒唐和模糊对你来说,你并不孤单这种简单的操作的复杂性是无理的和反效能的,但每个系统都有其缺点。

If that approach for passing data to templates sounded absurd and obfuscated to you, you’re not alone. The complexity involved in such a simple operation is unwarranted and counter-productive, but every system has its shortcomings.

官方文档在这里:

https://docs.djangoproject .com / en / dev / ref / templates / api /

所以,是的,我已经用Django编程了一段时间,我真的很喜欢解决问题的原因是因为它几乎是拜占庭式的复杂性,而不是一种霸气的方式。它有一吨的geegaws和doodads,可能不会马上显得有用;这些都是非常方便的,当你需要它,它会保持在你的方式,如果没有。

So but yeah, I have been programming with Django for a while, and one of the reasons I really like solving problems w/ it is because it is almost Byzantine in its complexity, but not in a domineering sort of way. It has a ton of geegaws and doodads that may not immediately appear useful; each of these either comes in extremely handy when you need it, and it will stay out of your way if not.

这里为你的结果是:上下文处理器是一个很好的例子。是的。

The upshot here for you is: context processors are a fine example of those. Yes.

这篇关于如何将变量传递给django中的所有模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆