将 Django 脆皮表单与 FormView 混合
Mixing Django crispy forms with FormView
我想在网站上制作一个 Django 联系表单,我发现 Django crispy forms 对此非常有用,但事实证明我不能像这样将它与 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 脆皮表单与 Django FormView 一起使用的任何建议?
这与脆皮形式没有任何关系。问题出在这段代码中,您将 ContactFormView
视为一种形式。
def contact(request):
contact_form = ContactFormView()
return render(request, 'myapp/contact.html', {'contact_form': contact_form})
但是,ContactFormView
不是表单,它是基于 class 的视图。您不需要定义另一个视图 contact
,您已经有一个视图 ContactFormView
.
更改您的 url 模式以使用基于 class 的视图:
url(r'^contact/$', ContactFormView.as_view(), name='contact')
然后在您的模板中,包含以下形式:
{{ form }}}
一旦视图与常规表单一起工作,请使用 crispy 标签,以便根据需要设置样式:
{% crispy form %}
我想在网站上制作一个 Django 联系表单,我发现 Django crispy forms 对此非常有用,但事实证明我不能像这样将它与 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 脆皮表单与 Django FormView 一起使用的任何建议?
这与脆皮形式没有任何关系。问题出在这段代码中,您将 ContactFormView
视为一种形式。
def contact(request):
contact_form = ContactFormView()
return render(request, 'myapp/contact.html', {'contact_form': contact_form})
但是,ContactFormView
不是表单,它是基于 class 的视图。您不需要定义另一个视图 contact
,您已经有一个视图 ContactFormView
.
更改您的 url 模式以使用基于 class 的视图:
url(r'^contact/$', ContactFormView.as_view(), name='contact')
然后在您的模板中,包含以下形式:
{{ form }}}
一旦视图与常规表单一起工作,请使用 crispy 标签,以便根据需要设置样式:
{% crispy form %}