首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何减少我的Django TemplateTags的重复性?

如何减少我的Django TemplateTags的重复性?
EN

Stack Overflow用户
提问于 2013-07-30 18:33:09
回答 1查看 85关注 0票数 0

我在Django应用程序中有一些模板,对于不同的标记/模板具有相同的功能。

Foo标记转到foo_template.html,boo标记转到boo_template.html,例如:

Foo标签:

代码语言:javascript
运行
复制
@register.inclusion_tag('foo_template.html', takes_context=True)
def foo(context, something):    
    sometng = something
    return {'something': sometng}

Boo标签:

代码语言:javascript
运行
复制
@register.inclusion_tag('boo_template.html', takes_context=True)
def boo(context, something):    
    sometng = something
    return {'something': sometng}

我如何使我的代码DRYer?在这种情况下,是否有更好的注册标记的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-30 20:41:57

请记住,装饰器只是包装函数的语法糖,它以原始的参数作为参数。因此,您可以将上下文函数不加修饰,然后定义两个包装器:

代码语言:javascript
运行
复制
def common(context, something):    
    sometng = something
    return {'something': sometng}

register.inclusion_tag('foo_template.html', takes_context=True, name='foo')(common)
register.inclusion_tag('boo_template.html', takes_context=True, name='bar')(common)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17954734

复制
相关文章

相似问题

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