2012-08-24 42 views
3

我想創建包含標籤並將其放置在頁面上,但它不工作。django inclusion_tag

我views.py:

from django.shortcuts import render_to_response, redirect 
from django import template 


register = template.Library() 

@register.inclusion_tag('weather.html') 
def weather(): 
    return {'city': 'angola'} 


def home(request): 
    return render_to_response('index.html') 

的index.html

<title> TITLE </title> 

Hi everyone! 

{% weather %} 

weather.html

weather is fine in {{city}} 

Django的調試頁說, 「無效的塊標籤: '天氣'」所以我想我把inclusion_tag的聲明放在錯誤的地方?我需要把它放在哪裏才能得到它的工作?

回答

6

模板標籤需要放在您應用的templatetags目錄中的模塊中。有關完整的詳細信息,請參閱自定義template tag docs的代碼佈局部分。

然後在使用標籤之前,您必須在模板中使用load標籤庫。

{% load my_tags %} 
{% weather %}