表单字段由https://github.com/earle/django-bootstrap/blob/master/bootstrap/templates/bootstrap/field_default.html中的自定义模板通过render_field
类中的BootStrapMixin
方法呈现。此处显示的代码: -
def render_field(self, field):
""" Render a named field to HTML. """
try:
field_instance = self.fields[field]
except KeyError:
raise NoSuchFormField("Could not resolve form field '%s'." % field)
bf = forms.forms.BoundField(self, field_instance, field)
output = ''
if bf.errors:
# If the field contains errors, render the errors to a <ul>
# using the error_list helper function.
# bf_errors = error_list([escape(error) for error in bf.errors])
bf_errors = ', '.join([e for e in bf.errors])
else:
bf_errors = ''
if bf.is_hidden:
# If the field is hidden, add it at the top of the form
self.prefix_fields.append(unicode(bf))
# If the hidden field has errors, append them to the top_errors
# list which will be printed out at the top of form
if bf_errors:
self.top_errors.extend(bf.errors)
else:
# Find field + widget type css classes
css_class = type(field_instance).__name__ + " " + type(field_instance.widget).__name__
# Add an extra class, Required, if applicable
if field_instance.required:
css_class += " required"
if field_instance.help_text:
# The field has a help_text, construct <span> tag
help_text = '<span class="help_text">%s</span>' % force_unicode(field_instance.help_text)
else:
help_text = u''
field_hash = {
'class' : mark_safe(css_class),
'label' : mark_safe(bf.label or ''),
'help_text' :mark_safe(help_text),
'field' : field_instance,
'bf' : mark_safe(unicode(bf)),
'bf_raw' : bf,
'errors' : mark_safe(bf_errors),
'field_type' : mark_safe(field.__class__.__name__),
}
if self.custom_fields.has_key(field):
template = get_template(self.custom_fields[field])
else:
template = select_template([
os.path.join(self.template_base, 'field_%s.html' % type(field_instance.widget).__name__.lower()),
os.path.join(self.template_base, 'field_default.html'), ])
# Finally render the field
output = template.render(Context(field_hash))
return mark_safe(output)
问题是我需要在mycustomclass
div中引入一个controls
css类,如下所示: -
<div class="control-group{% if errors %} error{% endif %}">
<label class="control-label">{{ label }}</label>
<div class="controls mycustomclass">
{{ bf }}
{% if errors %}
<span class="help-inline">{{ errors }}</span>
{% endif %}
<p class="help-block">{{ help_text }}</p>
</div>
</div> <!-- /clearfix -->
修改django-bootstrap的render_field method
以实现此目的的最佳方法是什么?
澄清
如下面的@okm所述,我应该css_class
携带自定义类,然后从'css': css_class
开始,我需要将{{ css }}
模板变量放在default_field.html
的适当位置{1}}。
例如,如果我有
class MyForm(BootstrapForm):
my_special_field = forms.ModelChoiceField(required=True)
并且
<div class="control-group{% if errors %} error{% endif %}">
<label class="control-label">{{ label }}</label>
<div class="controls {{ class }}">
{{ bf }}
{% if errors %}
<span class="help-inline">{{ errors }}</span>
{% endif %}
<p class="help-block">{{ help_text }}</p>
</div>
</div> <!-- /clearfix -->
将导致渲染的html显示
<div class="controls required">
但是,如何在表单代码中指定更多参数(比如使用** kwargs),以便可以在render_field
函数中使用它们?