0

表单字段由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)

问题是我需要在div中引入一个mycustomclasscss 类,如下所示:-controls

<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-bootstraprender_field method以实现此目的的最佳方法是什么?

澄清

正如下面@okm 所提到的,我应该css_class携带自定义类,然后因为'css': css_class,我需要将{{ css }}模板变量放在default_field.html.

例如,如果我有

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函数中使用?

4

0 回答 0