简体   繁体   English

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

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

I am playing with django-gentelella and try to add custom template tags to the project. 我正在玩django-gentelella,并尝试将自定义模板标签添加到项目中。

According to the latest Django documentation, one should add a "templatetags" directory, at the same level as models.py, views.py, etc. Also, an init .py file should be placed in the directory. 根据最新的Django文档,应该在“ models.py”,“ views.py”等同一级别上添加一个“ templatetags”目录。此外,应在该目录中放置一个init .py文件。

I added my template tags into a file called "template_tags.py" and restarted the server. 我将模板标签添加到名为“ template_tags.py”的文件中,然后重新启动了服务器。 In my templates, I load the file using "{% load template_tags %}" at the top of the file. 在模板中,我使用文件顶部的“ {%load template_tags%}”来加载文件。

Unfortunately, this does not work yet. 不幸的是,这还行不通。 According to the Django documentation, it is also required to add the template_filters to the INSTALLED APPS. 根据Django文档,还需要将template_filters添加到INSTALLED APPS。

My problem is that I cannot figure out how to get the right path in dot notation. 我的问题是我无法弄清楚如何以点符号表示正确的路径。 Can anyone point me in the right direction? 谁能指出我正确的方向?

/profiles/templatetags/custom_tags.py /profiles/templatetags/custom_tags.py

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

/profiles/templates/navbar.html /profiles/templates/navbar.html

{% load custom_tags %}
...

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

PS __init_.py should also be added to templatetags directory PS __init_.py也应该添加到templatetags目录

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

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