问题
I'm creating custom template tags for my django site. I've followed the django documentation for this and have created a templatetags
directory in my main application.
/project/apps/core/templatetags
-/__init__.py
-/core_extras.py
Since I am not sure if this is causing the problem, I should note I have this in my settings.py
sys.path.insert(0, join(PROJECT_ROOT, "apps"))
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_evolution',
'apps.core',
)
core_extras.py
from django import template
import settings
register = template.Library()
class SiteNameNode(template.Node):
def __init__(self):
def render(self, context):
site_name = getarrr(settings, "SITE_NAME", false)
if not site_name:
return "<!-- SITE_NAME not setting in Settings -->"
if settings.DEBUG:
return 'Debug || ' + site_name
return site_name
@register.tag(name="site_name")
def find_site_name(parser, token):
return SiteNameNode()
main.html
{% load core_extras %}
Error
In template /cygdrive/d/Users/Kin/BitNami DjangoStack projects/flipfinder/templates/main.html, error at line 1
'core_extras' is not a valid tag library: Template library core_extras not found, tried django.templatetags.core_extras,django.contrib.admin.templatetags.core_extras
Has anyone else run into a problem like this? Am I missing something obvious. I've went through and double checked everything, but I can't seem to find anyone with a similar problem.
回答1:
It's failing to import because of a syntax error. Django's templating system just swallows it up.
This line:
site_name = getarrr(settings, "SITE_NAME", false)
should be:
site_name = getattr(settings, "SITE_NAME", false)
来源:https://stackoverflow.com/questions/8177662/django-unable-to-find-my-custom-template-tags