Python 在模板中使用 Django 设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3430451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:02:52  来源:igfitidea点击:

Using Django settings in templates

pythondjangodjango-templates

提问by Roger

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

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

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

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

Thanks

谢谢

采纳答案by Geradeausanwalt

Let's say in your settings.pyfile you have:

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

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.

James 有一个在线教程 。

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

关于何时以及是否context处理器可以用这个非常站点的一些有用的信息 在这里

Inside your my_context_processors.pyfile you would:

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

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:

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

TEMPLATE_CONTEXT_PROCESSORS = (
    ...

    # yours
    'my_context_processors.some_context_processor',
)

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

在您的views.py, 视图中使用它,如下所示:

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)

回答by Brian Stoner

If you only need a setting or two for a couple views, Context Processor may be overkill since it will add them to ALL views in your app. But if it's used in a lot of templates, Contest Processor is the way to go.

如果您只需要为几个视图设置一两个,上下文处理器可能会过大,因为它会将它们添加到您应用程序中的所有视图中。但是如果它被用在很多模板中,那么 Contest Processor 是要走的路。

For the simple one off case just pass whatever setting you need from the view to the template:

对于简单的一次性案例,只需将您需要的任何设置从视图传递到模板:

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

def some_view(request):
    val = settings.SAVED_SETTING
    return render_to_response("index.html", {
        'saved_setting':val
    })

And access the setting in your template via:

并通过以下方式访问模板中的设置:

{{ saved_setting }}

回答by Bill Paetzke

If using a class-based view:

如果使用基于类的视图:

#
# in settings.py
#
YOUR_CUSTOM_SETTING = 'some value'

#
# in views.py
#
from django.conf import settings #for getting settings vars

class YourView(DetailView): #assuming DetailView; whatever though

    # ...

    def get_context_data(self, **kwargs):

        context = super(YourView, self).get_context_data(**kwargs)
        context['YOUR_CUSTOM_SETTING'] = settings.YOUR_CUSTOM_SETTING

        return context

#
# in your_template.html, reference the setting like any other context variable
#
{{ YOUR_CUSTOM_SETTING }}