在模板中使用Django设置 [英] Using Django settings in templates

查看:107
本文介绍了在模板中使用Django设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将一些配置信息放在我的settings.py文件中,比如站点名称,站点URL等。



如果我这样做,那么如何才能在模板中访问这些设置?



谢谢

解决方案

p>让我们在你的 settings.py 文件中说:

  SITE_URL ='www.mydomain.tld / athere /'
SITE_NAME ='我的网站'

如果您只需要一两个视图就可以:

  from django.shortcuts import render_to_response 
从django.conf导入设置

def my_view(request,...):
response_dict = {
'site_name':settings.SITE_NAME,
'site_url':settings.SITE_URL,
}
...
返回render_to_response('my_template_dir / my_template.html',response_dict)
pre>

如果您需要访问您可以编写一个上下文处理器来保存代码:



James有一个关于这个
的教程在线



在这个网站上有关上下文处理器何时和如果可用的一些有用信息
here



在你的 my_context_processors.py 文件,您将:

 从django.conf导入设置

def some_context_processor(request):
my_dict = {
'site_url':settings.SITE_URL,
'site_name':settings.SITE_NAME,
}

return my_dict

返回您的 settings.py ,通过执行以下操作激活它:

  TEMPLATE_C ONTEXT_PROCESSORS =(
...

#yours
'my_context_processors.some_context_processor',

在您的 views.py 中,使用这样的视图:

 从django.shortcuts导入render_to_response 
从django.template import RequestContext

def my_view(request,...):
response_dict = RequestContext(request)
...
#你仍然可以添加特定于此视图的变量
response_dict ['some_var_only_in_this_view'] = 42
。 ..
return render_to_response('my_template_dir / my_template.html',response_dict)


I want to be able to put certain configuration information in my settings.py file - things like the site name, site url, etc.

If I do this, how can I then access those settings in templates?

Thanks

解决方案

Let's say in your settings.py file you have:

SITE_URL='www.mydomain.tld/somewhere/'
SITE_NAME='My site'

If you need that in just one or two views:

from django.shortcuts import render_to_response
from django.conf import settings

def my_view(request, ...):
    response_dict = {
        'site_name': settings.SITE_NAME,
        'site_url': settings.SITE_URL,
    }
    ...
    return render_to_response('my_template_dir/my_template.html', response_dict)

If you need to access these across a lot of apps and/or views, you can write a context processor to save code:

James has a tutorial on this online.

Some useful information on the when and if of context processors is available on this very site here.

Inside your my_context_processors.py file you would:

from django.conf import settings

def some_context_processor(request):
    my_dict = {
        'site_url': settings.SITE_URL,
        'site_name': settings.SITE_NAME,
    }

    return my_dict

Back in your settings.py, activate it by doing:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...

    # yours
    'my_context_processors.some_context_processor',
)

In your views.py, make a view use it like so:

from django.shortcuts import render_to_response
from django.template import RequestContext

def my_view(request, ...):  
    response_dict = RequestContext(request)
    ...
    # you can still still add variables that specific only to this view
    response_dict['some_var_only_in_this_view'] = 42
    ...
    return render_to_response('my_template_dir/my_template.html', response_dict)

这篇关于在模板中使用Django设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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