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
- Browser-native date input field by kytta 4 weeks, 1 day ago
- Generate and render HTML Table by LLyaudet 1 month, 1 week ago
- My firs Snippets by GutemaG 1 month, 1 week ago
- FileField having auto upload_to path by junaidmgithub 2 months, 2 weeks ago
- LazyPrimaryKeyRelatedField by LLyaudet 2 months, 3 weeks 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.