django FormView 未创建对象或重定向到 success_url
django FormView not creating object or redirecting to success_url
尝试通过基于 class 的 FormView
实现 Django 表单并遵循文档对我不起作用(https://docs.djangoproject.com/en/1.11/topics/class-based-views/generic-editing/。项目是 1.11.9
models.py
class Contact(models.Model):
name = models.CharField(max_length=100)
company = models.CharField(max_length=100)
email = models.EmailField(unique=True)
message = models.TextField()
date_created = models.DateField(verbose_name="Created date", auto_now_add="True")
forms.py
from django import forms
from .models import Contact
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
labels = {
'company': 'Company or Organization'
}
exclude = ('date_created',)
views.py
from django.shortcuts import render
from django.core.mail import send_mail
from django.views.generic import FormView, TemplateView
from .forms import ContactForm
class ContactFormView(FormView):
form_class = ContactForm
template_name = "contact.html"
success_url = '/thanks/'
def form_valid(self,form):
message = "{name} from {company} / {email} said: ".format(
name=form.cleaned_data.get('name'),
company=form.cleaned_data.get('company'),
email=form.cleaned_data.get('email'))
message += "\n\n{0}".format(form.cleaned_data.get('message'))
send_mail(
subject='new message',
message=message,
from_email='foo@bar.com',
recipient_list=['foo@blah.com',]
)
form.save()
return super(ContactFormView, self).form_valid(form)
urls.py
from django.conf.urls import url
from django.views.generic import TemplateView
from . import views
from .views import ContactFormView
urlpatterns = [
url(r'^contact/?$', ContactFormView.as_view(), name="contact"),
url(r'^thanks/?$', views.thanks, name="thanks"),
url(r'^.*$', RedirectView.as_view(url=reverse_lazy('index'), permanent=True), name='home')
]
contact.html
{% extends 'base.html' %}
{% load humanize %}
{% block title %}Contact Us{% endblock %}
{% block content %}
<div class="wrapper">
<div id="content" class="container push-half">
<div class="col-lg-12">
<h1>Contact</h1>
<p class="push">Please use the form below to send us a message, and we'll get back to you as soon as we can.</p>
<div class="push cf">
<form action="." method="post" class="contact-form">
{% csrf_token %}
{{ form }}
<div class="clearfix"></div>
<input type="submit" value="Submit" id="contact-submit-btn" class="btn" />
</form>
</div>
</div>
</div>
</div>
{% endblock %}
首先,我有一个 'thanks'
视图,但是 success_url
在提交表单时只调用重定向视图返回主页。也尝试了 success_url = reverse_lazy('thanks')
但仍被重定向回主页。我是否需要显式覆盖 get_success_url
才能正常工作?可能相关,form.save()
没有从表单字段在数据库中创建新的 Contact
对象。谢谢
在 URL /contact
(没有尾部斜线)中,<form action="." ...>
表示请求将提交给 /
。
将操作更改为具体 post 联系人视图:
<form action="{% url 'contact' %}" method="post" class="contact-form">
通常,我会建议您停止将尾部斜杠设为可选,让 Django 从 /contact
重定向到 /contact/
。但是,由于您的包罗万象的重定向,这可能不起作用。
仔细考虑一下您是否真的想要这个包罗万象的重定向,它会微妙地改变 Django 的行为。您几乎肯定不希望 permanent=True
用于您的重定向 - 如果您将来添加另一个视图,浏览器将缓存重定向并继续重定向到主页。
尝试通过基于 class 的 FormView
实现 Django 表单并遵循文档对我不起作用(https://docs.djangoproject.com/en/1.11/topics/class-based-views/generic-editing/。项目是 1.11.9
models.py
class Contact(models.Model):
name = models.CharField(max_length=100)
company = models.CharField(max_length=100)
email = models.EmailField(unique=True)
message = models.TextField()
date_created = models.DateField(verbose_name="Created date", auto_now_add="True")
forms.py
from django import forms
from .models import Contact
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
labels = {
'company': 'Company or Organization'
}
exclude = ('date_created',)
views.py
from django.shortcuts import render
from django.core.mail import send_mail
from django.views.generic import FormView, TemplateView
from .forms import ContactForm
class ContactFormView(FormView):
form_class = ContactForm
template_name = "contact.html"
success_url = '/thanks/'
def form_valid(self,form):
message = "{name} from {company} / {email} said: ".format(
name=form.cleaned_data.get('name'),
company=form.cleaned_data.get('company'),
email=form.cleaned_data.get('email'))
message += "\n\n{0}".format(form.cleaned_data.get('message'))
send_mail(
subject='new message',
message=message,
from_email='foo@bar.com',
recipient_list=['foo@blah.com',]
)
form.save()
return super(ContactFormView, self).form_valid(form)
urls.py
from django.conf.urls import url
from django.views.generic import TemplateView
from . import views
from .views import ContactFormView
urlpatterns = [
url(r'^contact/?$', ContactFormView.as_view(), name="contact"),
url(r'^thanks/?$', views.thanks, name="thanks"),
url(r'^.*$', RedirectView.as_view(url=reverse_lazy('index'), permanent=True), name='home')
]
contact.html
{% extends 'base.html' %}
{% load humanize %}
{% block title %}Contact Us{% endblock %}
{% block content %}
<div class="wrapper">
<div id="content" class="container push-half">
<div class="col-lg-12">
<h1>Contact</h1>
<p class="push">Please use the form below to send us a message, and we'll get back to you as soon as we can.</p>
<div class="push cf">
<form action="." method="post" class="contact-form">
{% csrf_token %}
{{ form }}
<div class="clearfix"></div>
<input type="submit" value="Submit" id="contact-submit-btn" class="btn" />
</form>
</div>
</div>
</div>
</div>
{% endblock %}
首先,我有一个 'thanks'
视图,但是 success_url
在提交表单时只调用重定向视图返回主页。也尝试了 success_url = reverse_lazy('thanks')
但仍被重定向回主页。我是否需要显式覆盖 get_success_url
才能正常工作?可能相关,form.save()
没有从表单字段在数据库中创建新的 Contact
对象。谢谢
在 URL /contact
(没有尾部斜线)中,<form action="." ...>
表示请求将提交给 /
。
将操作更改为具体 post 联系人视图:
<form action="{% url 'contact' %}" method="post" class="contact-form">
通常,我会建议您停止将尾部斜杠设为可选,让 Django 从 /contact
重定向到 /contact/
。但是,由于您的包罗万象的重定向,这可能不起作用。
仔细考虑一下您是否真的想要这个包罗万象的重定向,它会微妙地改变 Django 的行为。您几乎肯定不希望 permanent=True
用于您的重定向 - 如果您将来添加另一个视图,浏览器将缓存重定向并继续重定向到主页。