簡體   English   中英

Django 1.3使用include_tag指定要在參數中使用的模板

[英]Django 1.3 specify the template to use in argument with inclusion_tag

我目前有2個templatetags用於幾乎同一件事:

@register.inclusion_tag('content.html')
def content(key, lazy=False, params=''):
    return {'key': key, 'lazy': js_bool(lazy), 'params': params}

@register.inclusion_tag('spec.html')
def spec(key, lazy=False, params=''):
    return {'key': key, 'lazy': js_bool(lazy), 'params': params}

我想將它們合並為僅使用更改的模板,我該怎么做?

您可以這樣操作:

def tag(key, lazy=False, params=''):
    return {'key': key, 'lazy': js_bool(lazy), 'params': params}

register.inclusion_tag('content.html', name='content')(tag)
register.inclusion_tag('spec.html', name='spec')(tag)

Python裝飾器不過是語法輔助器,您可以替換

@decorator
def function():
    pass

通過

def function():
    pass
function = decorator(function)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM