Crispy Forms和Django密码重置表单

时间:2014-04-16 15:46:08

标签: django django-crispy-forms

如何使用crispy表单助手来控制表单元素的布局,对于内置表单,如THIS帖子中提到的那样?

1 个答案:

答案 0 :(得分:0)

我认为它已经很晚了,但我希望它可以帮到你:

<强> forms.py

class PasswordResetFormExtra(auth.forms.PasswordResetForm):
    def __init__(self, *args, **kw):
        super(PasswordResetFormExtra, self).__init__(*args, **kw)

        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-2'
        self.helper.field_class = 'col-lg-8'        
        self.helper.layout = Layout(
            'email',
            Div(
               Submit('submit', 'Reset password', css_class='btn btn-default'),
               HTML('<a class="btn btn-default" href="/">Cancel</a>'),
               css_class='text-left',
            )
        ) 

urls.py

from django.contrib.auth.views import password_reset
from .forms import(
        PasswordResetFormExtra,
        )

    urlpatterns = patterns('',
         (r'^/accounts/password/reset/$', password_reset, {
         'template_name': 'my_templates/password_reset.html',
         'password_reset_form':PasswordResetFormExtra,
         }),

    )

<强> password_reset.html

{% load crispy_forms_tags %}
{% block content %}
{% crispy form %}
{% endblock %}