Rap*_*ael 6 django django-templates
假设我有以下模板:
<div id="openid_input_area">
{{ form.openid_identifier }}
<input name="bsignin" type="submit" value="{% trans "Sign in" %}">
</div>
Run Code Online (Sandbox Code Playgroud)
如何将css类添加到form.openid_identifier?
由于我遵守SOC原则并希望设计人员改进模板,我不希望在表单模型上执行此操作,而是在模板本身上执行此操作.
我在文档上找不到任何相关内容.有没有办法在模板上执行此操作?
Rap*_*ael 17
我设法为我的问题编写了一个简单的解决方案:
我写了一个自定义模板标签:
from django import template
register = template.Library()
def htmlattributes(value, arg):
attrs = value.field.widget.attrs
data = arg.replace(' ', '')
kvs = data.split(',')
for string in kvs:
kv = string.split(':')
attrs[kv[0]] = kv[1]
rendered = str(value)
return rendered
register.filter('htmlattributes', htmlattributes)
Run Code Online (Sandbox Code Playgroud)
而我在模板上所要做的就是:
{{ form.openid_identifier|htmlattributes:"class : something, id: openid_identifier" }}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5484 次 |
| 最近记录: |