【问题标题】:django inclusion_tagdjango 包含标签
【发布时间】:2012-08-20 10:43:14
【问题描述】:

我正在尝试创建包含标签并将其放置在页面上,但它不起作用。

我的意见.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 %}

天气.html

weather is fine in {{city}}

Django 调试页面说“无效的块标签:'天气'”所以我想我把包含标签的声明放在错误的地方?我需要把它放在哪里才能让它工作?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    模板标签需要放在应用程序的templatetags 目录中的模块中。详情请参阅自定义template tag docs 的代码布局部分。

    然后,您必须在模板中load您的标签库,然后才能使用您的标签。

    {% load my_tags %}
    {% weather %}
    

    【讨论】:

      猜你喜欢
      • 2017-04-06
      • 1970-01-01
      • 2012-12-22
      • 2012-07-05
      • 2014-07-29
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 2015-12-26
      相关资源
      最近更新 更多