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

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

问题描述

我希望能够在我的 settings.py 文件中放入某些配置信息 - 例如站点名称、站点 url 等.

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

谢谢

解决方案

假设在您的 settings.py 文件中:

SITE_URL='www.mydomain.tld/somewhere/'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,}...return render_to_response('my_template_dir/my_template.html', response_dict)

如果您需要在大量应用和/或视图中访问这些,您可以编写一个上下文处理器来保存代码:

James 有这方面的教程在线.>

这个站点上提供了一些关于上下文处理器何时和是否可用的有用信息此处.

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

from django.conf 导入设置def some_context_processor(请求):my_dict = {'site_url':settings.SITE_URL,'site_name':settings.SITE_NAME,}返回 my_dict

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

TEMPLATE_CONTEXT_PROCESSORS = (...#你的'my_context_processors.some_context_processor',)

在你的 views.py 中,让一个视图像这样使用它:

 from django.shortcuts import render_to_response从 django.template 导入 RequestContextdef my_view(request, ...):response_dict = RequestContext(请求)...# 您仍然可以添加仅针对此视图的变量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天全站免登陆