簡體   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