http://www.cnblogs.com/leomei91/p/7417198.html
首先说说什么叫全局变量,我们经常在html中使用{{ var }}这样的模板变量,这些变量是我们在视图函数中提前定义好的变量,通过render()等方法传递到模板中。
但是,还有一类变量,我们并没有在views.py中定义,也能在html中使用该变量,像这样的变量,就叫做【模版的】全局变量。
http://yshblog.com/blog/179
有时候,我们需要Templates模板页面可以使用一些变量。这些变量我们在views.py响应时没有返回设置的变量。例如,如下代码:
- #coding:utf-8
- from django.shortcuts import render
- def index(request):
- context = {}
- context['title'] = '测试标题'
- return render(request, 'index.html', context)
上面是某个views.py的方法之一。它将渲染index.html模版(Template)页面,并返回context字典。该字典是传入变量信息给前端页面。对应的index.html如下:
- <html>
- <head></head>
- <body>
- <h3>{{title}}</h3>
- <p>是否登录:{{request.user.is_authenticated}}</p>
- </body>
- </html>
响应结果除了有title变量值之外,还有是否登录信息。该登录信息来自request变量,问题是上面views.py中返回结果的context中没有写入request变量。而模版却可以获取该变量。
这个当时不是无中生有,我一步一步剖析给大家看。原理讲明白之后,就自然懂得如何设置模版(Templates)的全局变量或者叫默认变量。
render方法是render_to_response方法的简写方式。上面的views.py代码相当于如下:
- #coding:utf-8
- from django.shortcuts import render_to_response
- from django.template import RequestContext
- def index(request):
- context = {}
- context['title'] = '测试标题'
- return render_to_response('index.html', context, RequestContext(request))
如果去掉render_to_response的第三个参数,即RequestContext(request)部分。
渲染index.html模版页面就无法得到{{request.user.is_authenticated}}的值,即没有传递request变量给前端页面。很明显RequestContext很关键。
有关RequestContext的内容可以从Django官方文档查得。
该类实例化时会解析settings中的Templates设置中的context_processors配置。新建Django项目settings.py文件中默认的Templates设置如下:
- TEMPLATES = [
- {
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [],
- 'APP_DIRS': True,
- 'OPTIONS': {
- 'context_processors': [
- 'django.template.context_processors.debug',
- 'django.template.context_processors.request',
- 'django.contrib.auth.context_processors.auth',
- 'django.contrib.messages.context_processors.messages',
- ],
- },
- },
- ]
大家可发现context_processors有一系列设置,其中根据django.template.context_processors.request的路径找到Django的相关源码。
Django安装在Python的安装目录下Lib/site-packages/目录中,找到django/template/context_processors.py文件,打开可看到request方法:
- def request(request):
- return {'request': request}
该方法返回一个字典,key为request,value为request对象。很明显,render中的request对象就是通过加载settings中的context_processors列表方法得到字典项。
我们也可以采用这种方法,给Django项目设置全局的模版变量。例如,我的Django名称为myproject,在myproject/myproject目录中创建一个contexts.py文件,代码如下:
- #coding:utf-8
- from django.conf import settings
- # 得到语言设置
- def lang(request):
- return {'lang': settings.LANGUAGE_CODE}
该文件的方法需要request参数,最后需要返回一个字典即可。
再打开settings.py文件,在Templates中添加刚才写的方法引用:
- TEMPLATES = [
- {
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [],
- 'APP_DIRS': True,
- 'OPTIONS': {
- 'context_processors': [
- 'django.template.context_processors.debug',
- 'django.template.context_processors.request',
- 'django.contrib.auth.context_processors.auth',
- 'django.contrib.messages.context_processors.messages',
- # 自定义模版全局变量(默认变量)
- 'myproject.contexts.lang',
- ],
- },
- },
- ]
添加模版全局变量之后,我们可以在任意位置渲染模版页面无需再手动写相关代码即可使用该变量。
================================
下面来看看如何定义全局变量:
思路:
新建contexts.py文件-->修改settings.py文件-->在html中使用。
1.首先我们需要在项目根目录下的同名目录建立contexts.py文件
1 #!/bin/bash/python 2 # -*- coding: utf-8 -*- 3 # 全局变量 4 # 5 from django.conf import settings 6 7 def lang(request): 8 return {'lang': settings.LANGUAGE_CODE}
2.修改settings.py中的全局变量templates
61 'context_processors': [ 62 'django.template.context_processors.debug', 63 'django.template.context_processors.request', 64 'django.contrib.auth.context_processors.auth', 65 'django.contrib.messages.context_processors.messages', 66 'blog.contexts.lang', 67 ],
上述中'blog.contexts.lang'即是我们定义的方法
3.在模板中使用
<h3>全局变量lang:{{ lang }}</h3> <h3>是否登录:{{request.user.is_authenticated}}</h3>
request.user.is_authenticated为系统自带的全局变量。
tips:
在views.py中必须使用render()或其他能够定向到模板的方法,像HttpResponse()就不行!
附:页面图
http://yshblog.com/blog/179