django-crispy-form不呈现提交按钮,也不呈现表单操作属性

时间:2015-11-25 21:34:30

标签: django forms django-crispy-forms

我正在尝试使用django-cripsy来自定义我的表单。但似乎脆弱的表单不知道呈现提交按钮和表单操作属性。 这是我正在渲染表单的模板。(login.html)

{% extends '_base.html' %}
{% block content %}
{{ login_error_message }}

    {% load crispy_forms_tags %}
    {% crispy form form.helper  %}

{% endblock %}

这是视图

class LoginView(FormView):
    form_class = LoginForm
    template_name = "login.html"
    success_url = reverse_lazy('home')

    def form_valid(self, form):
        email = form.cleaned_data['email']
        password = form.cleaned_data['password']
        referrer = self.request.POST.get('referrer')
        user = authenticate(email=email, password=password)

        if user is not None:
            # if user.is_active:
            login(self.request, user)
            if referrer !="":
                self.success_url = referrer
            return super(LoginView, self).form_valid(form)
        else:

            return render(self.request, "login.html", {'form': form, 'login_error_message': "Invalid username and password."})

    def get_context_data(self, **kwargs):
        result = super(LoginView, self).get_context_data( **kwargs)
        param = self.request.GET.get('next', '')
        result.update({'param': param})
        return result

这是我的表格类

class LoginForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-form'
        self.helper.form_class = 'form-horizontal'
        self.helper.form_method = 'post'
        self.helper.form_action = 'login'

    email = forms.EmailField(max_length=100)
    password = forms.CharField(max_length=100)

我错过了什么?请帮忙。

2 个答案:

答案 0 :(得分:1)

关于缺少提交按钮: 你必须先添加它!

self.helper.add_input(Submit('submit', 'Submit'))

可以在Fundamentals documentation

的第4个清单中看到

答案 1 :(得分:0)

来自Django文档(https://docs.djangoproject.com/en/1.8/topics/templates/):

{# this won't be rendered #}

出于某种原因,您正在将模板输出为模板中的一系列Django注释。为什么?