Django无法找到我的自定义模板标签? [英] Django unable to find my custom template tags?

查看:648
本文介绍了Django无法找到我的自定义模板标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的django网站创建自定义模板标签。我已经遵循了django文档,并在我的主应用程序中创建了一个 templatetags 目录。

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

由于我不知道是否导致问题,我应该注意到我在 settings.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



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



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

有没有人遇到这样的问题?我错过了一些明显的事情。我已经通过了双重检查的一切,但我似乎找不到有类似问题的人。

推荐答案

由于语法错误,导入失败。 Django的模板系统只是吞下了它。

It's failing to import because of a syntax error. Django's templating system just swallows it up.

这一行:

site_name = getarrr(settings, "SITE_NAME", false)

应该是:

site_name = getattr(settings, "SITE_NAME", false)

这篇关于Django无法找到我的自定义模板标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆