0

这是我的 form.py 的代码。

class RegistrationForm(UserCreationForm):

    terms = forms.BooleanField(
        error_messages={'required': u'You must accept the terms and conditions'}, label=_(u"Terms&Conditions")
    )

    class Meta:
        model = User
        fields = ('first_name', 'last_name',)

    def __init__(self, *args, **kwargs):
        super(RegistrationForm, self).__init__(*args, **kwargs)
        self.fields['username'].icon_name = "icon-user"
        self.fields['first_name'].icon_name = "icon-user"
        self.fields['email'].icon_name = "icon-envelope"

    def save(self, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()

我想为我的字段添加例如: self.fields['first_name'].icon_name = "icon-envelope"

如何在模板中访问 {{ field.icon_name}}?

                <div class="control-group">
                  <label class="control-label"> {{ field.label }} {% if field.field.required %} <span class="required">*</span> {% endif %} {{ field.icon_name }}</label>
                  <div class="controls">
                      <div class="input-prepend">                         
                        <span class="add-on"><i class="{{ field.widgets.attrs.icon_name }}"></i></span>                                               
                        {% render_field field placeholder=field.label class="input-xlarge" %}                           
                      </div>
                      {% if field.errors %}
                        <p class="label label-warning">{{ field.errors.as_text }}</p>
                      {% endif %}
                  </div>
                </div>

谢谢!

4

0 回答 0