如何从Django中的任何视图向任何模板传递数据?

时间:2009-07-01 21:55:07

标签: django django-templates django-views

就像一个好的小编码器一样,我的所有Django模板都继承自base.html。现在我想在基础上添加一些功能,以便始终显示一些有趣的东西。一些用户统计信息,随机帖子或Feed等。

我的所有观点都是这样的:

def viewname(request) :
    template_vales = {}
    // Stuff
    return render_to_response('some_file_name.html', template_values)

如何制作以便始终为所有视图填充template_values的值?我是否必须在所有观点开始时这样做?如:

import utils

def viewname(request) :
    template_values = {}
    utils.addDefaults(template_values)
    // Stuff
    return render_to_response('some_file_name.html', template_values)

或者有更好的方法吗?

2 个答案:

答案 0 :(得分:9)

您应该使用上下文处理器:

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

http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/

在我的settings.py中,我为标准函数添加了几个函数(参见最后两个):

TEMPLATE_CONTEXT_PROCESSORS = (
  "django.core.context_processors.request",
  "django.core.context_processors.auth",
  "django.core.context_processors.debug",
  "django.core.context_processors.i18n",
  "thetrailbehind.context_processors.canonical_url",
  "thetrailbehind.context_processors.gmapkey",)

我添加的第一个定义了视图的规范URL,第二个在GMap键之间切换。这是函数:

def gmapkey(request):
  url = request.META['HTTP_HOST']
  key = ""
  if url == "127.0.0.1:8000":
    key = "ABQIAAAAGFSvsJjnPmsGb7IcfqoamBTpH3CbXHjuCVmaTc5MkkU4wO1RRhTaJZRNQLjBhGtJlm6eE4gJtku-Rw"   
  elif url ==  "192.168.11.3:8000":
    key = "ABQIAAAAGFSvsJjnPmsGb7IcfqoamBTm8-wcGRt2V-0p00qdRdGeyDhtGBSRTbk2s1ciA8vzdxGeAnqq6g-F4g"
  elif url ==  "192.168.11.17:7000":
    key="ABQIAAAAmHGaJpkZhJ6huJ93yfaYERTmT93Y0kqi8UE3J2QowoLz6rHdtxTHqeJ0nRoENl5LY5gCqHhRK9Yasg"
  elif url == "192.168.1.200:8000":
    key="ABQIAAAAmHGaJpkZhJ6huJ93yfaYERR5_sKpsr8Ui4YjC4HGOe8xaUDeVhSxGV1r1rIL1OvmVMAGUQBoUK0H2w"
  elif url == "192.168.1.73:8000":
    key = "ABQIAAAAGFSvsJjnPmsGb7IcfqoamBR7_CRKSBu49YjvDOLq_-DZQHSIYBSip9sO5IHlFIoZMtDpVcduFQCnWg"
  elif url == "www.trailbehind.com":
    key="ABQIAAAAGFSvsJjnPmsGb7IcfqoamBQxFGSDsNggDdRtUnAb8L8sJ910FhSKwoOpNaUlGCQIhyl6Dy5Cbyb0lQ"
  elif url == "dev.trailbehind.com":
    key="ABQIAAAAmHGaJpkZhJ6huJ93yfaYERQzqIbhF_xOwOwM1oDO_kQqYhag7BRsoTInq2lBuE7fsgDN2xfyD2IL5A"
  elif url == "trailbehind.com":
    key = "ABQIAAAAGFSvsJjnPmsGb7IcfqoamBQL9YYTGyB2pLTiscy54DOfsaXeHBQqMBmq7UvWAZVenmRMtNr_bo3TMQ"
  elif url == "tenuki.trailbehind.com":
    key = "ABQIAAAAGFSvsJjnPmsGb7IcfqoamBQ5SkJUKVREyqcvaNQJsRscGi2yVhSj0mJSTasDiWec8Awxb_TUxOdElw"
  elif url == "cabin.trailbehind.com":
    key = "ABQIAAAAmHGaJpkZhJ6huJ93yfaYERSU-76xxg1tvy-8taAiiF1qqcGi1xSmjUhmAs_v2XAuGxKX_Y-4-gDP3Q"
  elif url == "ec2-174-129-167-234.compute-1.amazonaws.com":
    key = "ABQIAAAAmHGaJpkZhJ6huJ93yfaYERStHq7nubctzsNDgkYc34LoSNrRNhQVCNy2KFFm2BT1sG2yrXrw38ycNg"

答案 1 :(得分:4)

为此,我使用上下文处理器。例如,如果我想为每个视图获取变量MEDIA_URL,我就像这样定义context_processors.py

def media_url(request):
    from django.conf import settings
    return {'MEDIA_URL': settings.MEDIA_URL}

settings.py中你必须

TEMPLATE_CONTEXT_PROCESSORS = (
    ....
    'django.core.context_processors.request',
    'myaplication.context_processors.menuitems',
)

在视图中,您必须拥有render_to_responsecontext_instance=RequestContext(request) 例如:

def my_view(request):
return render_to_response('base.html',{},
                              context_instance=RequestContext(request))