27๐
โ
Use context_processor
, do as below and you should be able to access os.environ['DJANGO_SETTINGS_MODULE']
as SETTING_TYPE
in templates. Example you can use {% if SETTING_TYPE == "settings.staging" %}
project/context_processors.py
import os
def export_vars(request):
data = {}
data['SETTING_TYPE'] = os.environ['DJANGO_SETTINGS_MODULE']
return data
project/settings.py (your actual settings file)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'project.context_processors.export_vars',
...
]
}
}
]
๐คrrmerugu
7๐
Extending on @vinay kumarโs comment, you can create a custom template filter in the following way:
application/template_tags_folder/template_tags_file.py
from django.template.defaulttags import register
import os
@register.filter
def env(key):
return os.environ.get(key, None)
And then in your template you can access it this way:
template.html
{% if 'DJANGO_SETTINGS_MODULE'|env == 'app_name.staging_settings' %}
Finally I leave a django docs reference and a stackoverflow reference for creating custom template tags and filters in django.
๐คManuel Montoya
- [Django]-Django: For Loop to Iterate Form Fields
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
- [Django]-How can I chain Django's "in" and "iexact" queryset field lookups?
Source:stackexchange.com