Reputation: 21
I am following a tutorial and I am unable to understand some lines in it:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from . models import Page
from .forms import ContactForm
def index(request, pagename):
pagename = '/' + pagename
pg = get_object_or_404(Page, permalink=pagename)
context = {
'title': pg.title,
'content': pg.bodytext,
'last_updated': pg.update_date,
'page_list': Page.objects.all(),
}
# assert False
return render(request, 'pages/page.htm', context)
def contact(request):
submitted = False
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
#assert False
return HttpResponseRedirect('/contact?submitted=True')
else:
form = ContactForm()
if 'submitted' in request.GET:
submitted = True
return render(request,'pages/contact.htm',{'form': form, 'page_list': Page.objects.all(), 'sbmitted': submitted})
The above is pages/view.py
file
{% extends "pages/page.htm" %}
{% block title %} Contact us {% endblock title %}
{% block content %}
<h1>Contact us</h1>
{% if submitted %}
<p class="success">
Your message was submitted successfully. Thankm you.
</p>
{% else %}
<form action="" method="post" novalidate>
<table>
{{ form.as_table }}
<tr>
<td> </td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
{% csrf_token %}
</form>
{% endif %}
{% endblock content %}
The above is pages/contact.htm
file
So, what is the meaning of
if requested.method == 'POST':
and why is there the following check?
if submitted in request.GET:
submitted=True
Upvotes: 0
Views: 11194
Reputation: 21
And there should be
if request.GET.get('submitted') == "True":
submitted = True
Instead of
if submitted in request.GET:
submitted=True
Upvotes: 1
Reputation: 98
www.mydomain.com/Form?Key1=xxxxx&Key2=yyyyyyy
With POST the data is sent "hidden". For example, in a login form you don't want the password to be visible in the url. That's why in these forms is used as a method of sending POST.
if request.method == 'POST':
that validates that the data you are sending is in POST format
2.
else:
form = ContactForm()
if 'submitted' in request.GET:
submitted = True
This means that if the sending method was not POST, but GET, look if within the information that was sent there is a submitted
parameter and if so, set its value as True.
Upvotes: 0