Useage:
{% load setting %}
{% setting DEBUG %}
or...
{% setting MEDIA_ROOT %}
You get the gist.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from django import template
from django.conf import settings
register = template.Library()
@register.tag
def setting ( parser, token ):
try:
tag_name, option = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents[0]
return SettingNode( option )
class SettingNode ( template.Node ):
def __init__ ( self, option ):
self.option = option
def render ( self, context ):
# if FAILURE then FAIL silently
try:
return str(settings.__getattr__(self.option))
except:
return ""
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
"be generic" (c)
#
In fact, I want settings so often that I put that into context:
settings.py
context_processors.py
#
from django.conf import settings register = template.Library()
#
The decorator is missing an underscore:
@register.simple_tag
#
In your template, the key name must be quoted:
{% setting 'DEBUG' %}
#
Please login first before commenting.