python - 将 Django 脆皮表单与 FormView 混合

标签 python django formview django-crispy-forms

我想在网站上制作一个 Django 联系表单,我发现 Django 脆皮表单对此非常有用,但事实证明我不能像这样将它与 Django FormView 混合使用。 Crispy 表单在前端布局方面做得非常出色,但我无法从填充的表单中获取任何信息到我的 Django 应用程序。我已经尝试过这个教程:Simple Django email form using CBV ,但它已经过时了并且根本没有帮助。这是我的 forms.py:

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

class ContactForm(forms.Form):

    name = forms.CharField(
        label = "Name:",
        max_length = 80,
        required = True,
    )

    email = forms.CharField(
        label = "E-mail:",
        max_length = 80,
        required = True,
    )

    subject = forms.CharField(
        label = "Subject:",
        max_length = 80,
        required = True,
    )

    message = forms.CharField(
        widget = forms.Textarea,
        label = "Message:",
        required = True,
    )

    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.add_input(Submit('send', 'Send'))

views.py:

from django.shortcuts import render
from myapp.forms import ContactForm

from django.core.mail import send_mail
from django.views.generic.edit import FormView

class ContactFormView(FormView):

    form_class = ContactForm
    template_name = "myapp/contact.html"
    success_url = '/email_sent/'

    def form_valid(self, form):
        message = "{name} / {email} said: ".format(
            name=form.cleaned_data.get('name'),
            email=form.cleaned_data.get('email'))
        message += "\n\n{0}".format(form.cleaned_data.get('message'))
        send_mail(
            subject=form.cleaned_data.get('subject').strip(),
            message=message,
            from_email='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98fbf7f6ecf9fbecb5fef7eaf5d8f5e1f9e8e8b6fbf7f5" rel="noreferrer noopener nofollow">[email protected]</a>',
            recipient_list=['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5d8ccd8d4dcd9f5d8ccd1dad8d4dcdb9bd6dad8" rel="noreferrer noopener nofollow">[email protected]</a>'],
        )
        return super(ContactFormView, self).form_valid(form)

def contact(request):
    contact_form = ContactFormView()
    return render(request, 'myapp/contact.html', {'contact_form': contact_form})

和我的模板 contact.html:

{% extends "layout.html" %}
{% load crispy_forms_tags %}

{% block title %}Contact{% endblock %}

{% block content %}
    <h2><b>Contact</b></h2>
    <div class="container">
        <p>You can e-mail me at: <a href="mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bed3c7d3dfd7d2fed3c7dad1d3dfd7d090ddd1d3" rel="noreferrer noopener nofollow">[email protected]</a>"><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e9849084888085a984908d8684888087c78a8684" rel="noreferrer noopener nofollow">[email protected]</a></a></p>
        <br>
        <p>Or simply fill the form below:</p>
        <br>
        {% crispy contact_form %}
    </div>
{% endblock %}

我得到的是:

Exception Type: TypeErrorException Value:   
'ContactFormView' object is not iterable

关于如何将 Django 脆皮表单与 Django FormView 一起使用有什么建议吗?

最佳答案

这与脆皮形式没有任何关系。问题出在这段代码中,您将 ContactFormView 视为表单。

def contact(request):
    contact_form = ContactFormView()
    return render(request, 'myapp/contact.html', {'contact_form': contact_form})

但是,ContactFormView 不是表单,而是基于类的 View 。您不需要定义另一个 View contact,您已经有一个 View ContactFormView

更改您的 url 模式以使用基于类的 View :

url(r'^contact/$', ContactFormView.as_view(), name='contact')

然后在您的模板中包含表单:

{{ form }}}

一旦 View 使用常规表单,请使用脆皮标签,以便其样式符合您的意愿:

{% crispy form %}

关于python - 将 Django 脆皮表单与 FormView 混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28413541/

相关文章:

python - Django 教程中的 __str__(self) 返回对象而不是文本

c# - FormView 不进入插入模式。为什么?

django - FormView 未在 Django 中保存数据

python - "settings.DATABASES is improperly configured"与 django 1.6

python - Login_Required Django 不使用 FormView

python - 根据索引列表更改 Pandas 列的值

python - 使用两列的第一个和最后一个值并根据条件生成新的数据框

python - 使用Python字典值返回键作为结果

python - turtle 图形脚本在代码运行时不断崩溃

python - 类型对象 'Notification' 没有属性 'object'