Meet Mehta
Meet Mehta

Reputation: 21

request.method and request.GET in Django

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>&NonBreakingSpace;</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

Answers (2)

Meet Mehta
Meet Mehta

Reputation: 21

  • request.method gives which method is to submit the form so the first thing checks if the form is submitted with the post method
  • request.GET returns a context(similar to dictionary in python) of all the variables passed by GET method
  • And there should be

    if request.GET.get('submitted') == "True":

    submitted = True
    

Instead of

if submitted in request.GET:
    submitted=True
  • request.GET.get('submitted') gives value of submitted passed in url
  • And the thing to note is that both submitted in above code are different the former one is a key in context(similar to dictionary ) and the later one is a variable in views.py

Upvotes: 1

Nato
Nato

Reputation: 98

  1. you can send the data via GET or POST. With GET you send the data through the URL. e.g.

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

Related Questions