Reputation: 1575
trying to implement a django form via class-based FormView
and following the docs isn't working for me (https://docs.djangoproject.com/en/1.11/topics/class-based-views/generic-editing/. Project is 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='[email protected]',
recipient_list=['[email protected]',]
)
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 %}
first, I have a 'thanks'
view, but success_url
on form submit just calls the redirect view back to home. also tried success_url = reverse_lazy('thanks')
but still being redirected back home. do I need to explicitly overwrite get_success_url
for it to work? possibly related, form.save()
is not creating a new Contact
object in db from form fields. thanks
Upvotes: 1
Views: 1351
Reputation: 309089
On the URL /contact
(without a trailing slash), <form action="." ...>
means the request will be submitted to /
.
Change the action to specifically post to the contact view:
<form action="{% url 'contact' %}" method="post" class="contact-form">
Usually, I would suggest you stop making the trailing slash optional, and let Django redirect from /contact
to /contact/
. However this might not work because of your catch-all redirect.
Think carefully about whether you really want this catch-all redirect, it's going to subtlety change Django behaviour. You almost certainly don't want permanent=True
for your redirect - if you add another view in future, browsers will have cached the redirect and continue to redirect to the home page.
Upvotes: 1