繁体   English   中英

如何正确地将模板标签添加到Django项目

[英]How to properly add template tags to a django project

我正在玩django-gentelella,并尝试将自定义模板标签添加到项目中。

根据最新的Django文档,应该在“ models.py”,“ views.py”等同一级别上添加一个“ templatetags”目录。此外,应在该目录中放置一个init .py文件。

我将模板标签添加到名为“ template_tags.py”的文件中,然后重新启动了服务器。 在模板中,我使用文件顶部的“ {%load template_tags%}”来加载文件。

不幸的是,这还行不通。 根据Django文档,还需要将template_filters添加到INSTALLED APPS。

我的问题是我无法弄清楚如何以点符号表示正确的路径。 谁能指出我正确的方向?

/profiles/templatetags/custom_tags.py

@register.filter(name='getLocalTimeDifference')
def getLocalTimeDifference(value):
    value = value.replace(..)
    return value

/profiles/templates/navbar.html

{% load custom_tags %}
...

<div class="notification-meta">
    <small class="timestamp">{{ notification.timesince | getLocalTimeDifference}} before </small>
</div>

PS __init_.py也应该添加到templatetags目录

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM