Вопрос

I'm trying to do something really silly: show some settings variables in a Django template (using Django 1.5). So, if I try this:

<p>Timezone: {{ TIME_ZONE }}</p>

I get the timezone defined in my settings file. So far, so good.

But now, let say I define this new setting:

FOO = 'bar'

And try:

<p>Foo: {{ FOO }}</p>

I can't see the variable's value. Why?

I know I can pass variables to templates from views, but what if I want to define a name and description for my project in just one place and show them in any template? This is one of the simple tasks I want to do.

This kind of problem suggests me I'm not doing things properly (meaning properly "how Django thinks that should be done"), but I don't know why.

Это было полезно?

Решение

You have a TIME_ZONE variable available in your templates because you have django.core.context_processors.tz context processor enabled in your settings.
Reference: https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#get-current-timezone.

For other settings you'd need to write your own context processor or send your settings values from the view.
You can import your project's settings like that: from django.conf import settings.
Docs advise to use it instead of your local settings file.

You can find details here: https://docs.djangoproject.com/en/dev/topics/settings/#using-settings-in-python-code

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top