Django – New object is not created if html action argument is provided
Question:
I have simple contact form:
<form method="post">
{% csrf_token %}
{{ form_email.as_div }}
{{ form_message.as_div }}
<button name="submit">Send</button>
</form>
When text is provided in both fields (email and message) and then the user click on submit button, the data should be saved in data base.
View function:
def index(request):
"""The home page."""
# Send a message.
if request.method != 'POST':
# No data submitted; create a blank form.
form_email = EmailForm()
form_message = EmailMessageForm()
else:
# POST data submitted; proecess data.
form_email = EmailForm(data=request.POST)
form_message = EmailMessageForm(data=request.POST)
if form_email.is_valid() and form_message.is_valid():
form_email.save()
email = Email.objects.last()
message = form_message.save(commit=False)
message.email = email
message.save()
# Display a blank or invalid form.
context = {'form_email': form_email, 'form_message': form_message}
return render(request, 'home/index.html', context)
Until now everything works as expected, but when I add action argument in order to redirect the user to the ‘Thank you’ page if form has been sent, the data is not saved.
<form action="{% url 'home:contact_thank_you' %}" method="post">
{% csrf_token %}
{{ form_email.as_div }}
{{ form_message.as_div }}
<button name="submit">Send</button>
</form>
Do you have any idea why the data is not saved when I add the action argument?
Answers:
I resolved the problem. I added redirect() function in view function:
return redirect('home:contact_thank_you')
I have simple contact form:
<form method="post">
{% csrf_token %}
{{ form_email.as_div }}
{{ form_message.as_div }}
<button name="submit">Send</button>
</form>
When text is provided in both fields (email and message) and then the user click on submit button, the data should be saved in data base.
View function:
def index(request):
"""The home page."""
# Send a message.
if request.method != 'POST':
# No data submitted; create a blank form.
form_email = EmailForm()
form_message = EmailMessageForm()
else:
# POST data submitted; proecess data.
form_email = EmailForm(data=request.POST)
form_message = EmailMessageForm(data=request.POST)
if form_email.is_valid() and form_message.is_valid():
form_email.save()
email = Email.objects.last()
message = form_message.save(commit=False)
message.email = email
message.save()
# Display a blank or invalid form.
context = {'form_email': form_email, 'form_message': form_message}
return render(request, 'home/index.html', context)
Until now everything works as expected, but when I add action argument in order to redirect the user to the ‘Thank you’ page if form has been sent, the data is not saved.
<form action="{% url 'home:contact_thank_you' %}" method="post">
{% csrf_token %}
{{ form_email.as_div }}
{{ form_message.as_div }}
<button name="submit">Send</button>
</form>
Do you have any idea why the data is not saved when I add the action argument?
I resolved the problem. I added redirect() function in view function:
return redirect('home:contact_thank_you')