Explore. Dream. Discover.

Samuel Chen's life

Twenty years from now you will be more disappointed by
the things that you didn't do than by the ones you did do.
So throw off the bowlines. Sail away from the safe harbor.
Catch the trade winds in your sails.

Explore. Dream. Discover.
—— Mark Twain

2011年3月5日星期六

Django 如何在模板(template)中使用settings中预定义的变量


在Django中编写模板(template)的时候,有时候可能会用到settings中设定的变量,比如说STATIC_URL。此时,如果你直接使用 {{ STATIC_URL }} 是取不到值的。那么怎么才能在模板中使用呢?难道非得在每个view中添加到context中吗?

答案是否定的。根据Django文档中所描述,我们至少有两种方法可以直接使用。http://docs.djangoproject.com/en/dev/howto/static-files/#referring-to-static-files-in-templates

方法1:

使用 {% load static %} 载入 static 模块,然后使用 {% get_static_prefix %} 就可以了。
{% load static %}

<img src="{% get_static_prefix %}images/hi.jpg" />
当然也可以将其定义为变量以多次使用
{% load static %}

{% get_static_prefix as STATIC_PREFIX %}

<img src="{{ STATIC_PREFIX }}images/hi.jpg" />
<img src="{{ STATIC_PREFIX }}images/hi2.jpg" />

方法2:

这个方法是推荐的方法,直接使用 RequestContext 来传递。其原因就是因为settings中定义的变量都会在request中传递,但是response的时候是没有这些context的,所以Django专门定义了RequestContext来帮你组合(其实你自己也可以做这件事)。


#注意这里是django.template,由此可见是专门为template设计的
from django.template import RequestContext

def some_view(request):
    # ...
    return render_to_response('my_template.html',
                              my_data_dictionary,
                              context_instance=RequestContext(request))

这个实际作用就是将request中的context全部都加到response的context中去。

这样,你就可以直接在模板中使用{{ STATIC_URL }} 来使用了,当然其他的变量也是可以的,不需要每个都单独去load了。

标签: ,

0 条评论:

发表评论

订阅 帖子评论 [Atom]

指向此帖子的链接:

创建链接

<< 主页