django FormView not creating object or redirecting to success_url
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 += "nn0".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 %
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
django django-forms django-views django-class-based-views
add a comment |
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 += "nn0".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 %
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
django django-forms django-views django-class-based-views
Can you show your template?
– Daniel Roseman
Nov 14 '18 at 17:29
@DanielRoseman added template
– Chris B.
Nov 14 '18 at 17:32
And what was the exact URL you went to to display the form? Did it end with a slash?
– Daniel Roseman
Nov 14 '18 at 17:34
add a comment |
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 += "nn0".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 %
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
django django-forms django-views django-class-based-views
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 += "nn0".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 %
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
django django-forms django-views django-class-based-views
django django-forms django-views django-class-based-views
edited Nov 14 '18 at 17:32
Chris B.
asked Nov 14 '18 at 17:24
Chris B.Chris B.
47531432
47531432
Can you show your template?
– Daniel Roseman
Nov 14 '18 at 17:29
@DanielRoseman added template
– Chris B.
Nov 14 '18 at 17:32
And what was the exact URL you went to to display the form? Did it end with a slash?
– Daniel Roseman
Nov 14 '18 at 17:34
add a comment |
Can you show your template?
– Daniel Roseman
Nov 14 '18 at 17:29
@DanielRoseman added template
– Chris B.
Nov 14 '18 at 17:32
And what was the exact URL you went to to display the form? Did it end with a slash?
– Daniel Roseman
Nov 14 '18 at 17:34
Can you show your template?
– Daniel Roseman
Nov 14 '18 at 17:29
Can you show your template?
– Daniel Roseman
Nov 14 '18 at 17:29
@DanielRoseman added template
– Chris B.
Nov 14 '18 at 17:32
@DanielRoseman added template
– Chris B.
Nov 14 '18 at 17:32
And what was the exact URL you went to to display the form? Did it end with a slash?
– Daniel Roseman
Nov 14 '18 at 17:34
And what was the exact URL you went to to display the form? Did it end with a slash?
– Daniel Roseman
Nov 14 '18 at 17:34
add a comment |
1 Answer
1
active
oldest
votes
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.
thanks shouldn't have missed that. and you make a helpful point about the catch-all redirect
– Chris B.
Nov 14 '18 at 17:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305681%2fdjango-formview-not-creating-object-or-redirecting-to-success-url%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
thanks shouldn't have missed that. and you make a helpful point about the catch-all redirect
– Chris B.
Nov 14 '18 at 17:57
add a comment |
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.
thanks shouldn't have missed that. and you make a helpful point about the catch-all redirect
– Chris B.
Nov 14 '18 at 17:57
add a comment |
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.
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.
answered Nov 14 '18 at 17:35
AlasdairAlasdair
182k26309311
182k26309311
thanks shouldn't have missed that. and you make a helpful point about the catch-all redirect
– Chris B.
Nov 14 '18 at 17:57
add a comment |
thanks shouldn't have missed that. and you make a helpful point about the catch-all redirect
– Chris B.
Nov 14 '18 at 17:57
thanks shouldn't have missed that. and you make a helpful point about the catch-all redirect
– Chris B.
Nov 14 '18 at 17:57
thanks shouldn't have missed that. and you make a helpful point about the catch-all redirect
– Chris B.
Nov 14 '18 at 17:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305681%2fdjango-formview-not-creating-object-or-redirecting-to-success-url%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you show your template?
– Daniel Roseman
Nov 14 '18 at 17:29
@DanielRoseman added template
– Chris B.
Nov 14 '18 at 17:32
And what was the exact URL you went to to display the form? Did it end with a slash?
– Daniel Roseman
Nov 14 '18 at 17:34