首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django inclusion_tag

django inclusion_tag
EN

Stack Overflow用户
提问于 2012-08-25 05:07:22
回答 1查看 7.6K关注 0票数 9

我试图创建包含标签,并将其放在页面上,但这是不工作的。

我的views.py:

代码语言:javascript
复制
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

代码语言:javascript
复制
<title> TITLE </title>

Hi everyone!

{% weather %}

weather.html

代码语言:javascript
复制
weather is fine in {{city}}

Django调试页面显示“无效块标记:‘天气’”,所以我猜我把inclusion_tag声明放错了地方?我需要把它放在哪里才能让它工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-25 05:35:47

模板标签需要放在应用程序的templatetags目录中的一个模块中。有关完整的详细信息,请参阅自定义template tag docs的代码布局部分。

然后,您必须在使用标记之前在模板中load您的标记库。

代码语言:javascript
复制
{% load my_tags %}
{% weather %}
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12116664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档