【问题标题】:Mixing Django crispy forms with FormView将 Django 脆表单与 FormView 混合
【发布时间】:2015-04-09 09:57:47
【问题描述】:

我想在网站上制作一个 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='contact-form@myapp.com',
            recipient_list=['mymail@mydomain.com'],
        )
        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:mymail@mydomain.com">mymail@mydomain.com</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 FormView 中使用 Django 脆表单有什么建议吗?

【问题讨论】:

  • 我不知道 django 脆皮表格,但你为什么不能使用默认的?他们似乎在做你想做的事
  • 老实说,我没有尝试过 django 默认表单。 Crispy 很容易实现,他们使用 bootstrap3,就像我在我的网站上一样:D
  • 您可以调整 django 表单以使用 bootstrap 3 类,方法是创建模板标签以在模板中添加 css 类(这就是我所做的),或者直接修改 css 的属性表单类。但再说一次,我从来没有用过脆的形式,所以我不知道它们好不好。
  • 好吧,看来我也必须这样做。感谢您的回答!

标签: python django formview django-crispy-forms


【解决方案1】:

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

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

但是,ContactFormView 不是表单,它是基于类的视图。你不需要定义另一个视图contact,你已经有一个视图ContactFormView

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

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

然后在您的模板中,包含以下内容:

{{ form }}}

一旦视图与常规表单一起工作,请使用脆标签,以便按照您的意愿设置样式:

{% crispy form %}

【讨论】:

  • 就是这样,我没弄明白。谢谢!
猜你喜欢
  • 2020-08-21
  • 2019-12-22
  • 2017-10-24
  • 2012-09-08
  • 2014-02-28
  • 1970-01-01
  • 2016-07-30
  • 2013-07-01
  • 2014-12-17
相关资源
最近更新 更多