0

My project directory looks like this. settings_value.py has a template tag, named 'settings_value' in it. In my settings.py I added 'itslogical.templatetags' to INSTALLED_APPS. I try to use it in logicalhp/home.html, but it says the tag doesn't exist. I'm using code from this answer (I changed the name from 'value_from_settings' to 'settings_value').

.
├── internetparse
│   └── ...
├── itslogical
│   ├── settings.py
│   ├── templates
│   │   └── itslogical
│   │       └── base.html
│   └─── templatetags
│       ├── __init__.py
│       └── settings_value.py
├── logicalhp
│   ├── templates
│   │   └── logicalhp
│   │       └── home.html
│   └── views.py
└── manage.py

Let me know if you need anything else. What am I missing here?

Edit: added code and updated error. This is based on @Dan's answer.

500 ERROR:
'settings_value' is not a valid tag library: Template library settings_value not found
Template library settings_value not found, tried django.templatetags.settings_value ...
#!/usr/bin/env python

from django import template
from django.conf import settings

# Include settings from here in templates
register = template.Library()

# settings value
@register.tag
def settings_value(parser, token):
    try:
        # split_contents() knows not to split quoted strings.
        tag_name, var = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
    return ValueFromSettings(var)

class ValueFromSettings(template.Node):
    def __init__(self, var):
        self.arg = template.Variable(var)

    def render(self, context):
        return settings.__getattr__(str(self.arg))
Community
  • 1
  • 1
Brigand
  • 84,529
  • 20
  • 165
  • 173

3 Answers3

1

You shouldn't add the templatetags directory to installed apps. You should put the templatetags directory inside an existing app, and add that to installed apps.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I moved it, and updated the question. The error is weird, so maybe I did something wrong in the code... – Brigand Jan 01 '13 at 20:44
1

Try to move templatetags folder to logicalhp

Chris
  • 517
  • 1
  • 9
  • 22
0

Part of the problem was a typo in my settings.py (wrote the 'logicalhp.templatetags' when it was in 'itslogical'). The larger problem was that it was trying to get the attribute "STATIC_URL" from my settings. It included the quotes, so it was effectively settings.__getattr__('"STATIC_URL"').

To fix it, I added a strip.

return settings.__getattr__(str(self.arg)) #before
return settings.__getattr__(str(self.arg).strip('"')) #after

(By the way, it's not like you can omit the quotes in the template; else it think's its a variable. )

Brigand
  • 84,529
  • 20
  • 165
  • 173