2017-08-11 62 views
0

我正在玩django-gentelella並嘗試添加自定義模板標籤到項目中。如何正確添加模板標籤到django項目

根據最新的Django文檔,應該添加一個「templatetags」目錄,與models.py,views.py等級相同。此外,init .py文件應放置在目錄中。

我將我的模板標籤添加到名爲「template_tags.py」的文件中並重新啓動服務器。在我的模板中,我使用文件頂部的「{%load template_tags%}」加載文件。

不幸的是,這還不行。根據Django文檔,還需要將template_filters添加到INSTALLED APPS。

我的問題是,我無法弄清楚如何在點符號中找到正確的路徑。任何人都可以將我指向正確的方向嗎?

+0

您不會將模板標籤添加到INSTALLED_APPS,您添加了它們所在的應用程序。不應該包含任何虛線路徑。 –

回答

1

/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> 

附: __init_.py也應該添加到templatetags目錄中

+0

我試過了,現在我得到:'template_tags'不是已註冊的標籤庫。必須是一個: admin_modify admin_static admin_urls 緩存 國際化 本地化 日誌 靜態 staticfiles TZ – Jabb

+0

是你的Python文件 'template_tags.py' 的名字 admin_list? –

+0

是的。真奇怪。 – Jabb