← Back to Django Course | Chapter 7: Django Forms | Lesson 6 of 11

POST Requests को Handle करना

POST request handle करने का मतलब है view यह check करे कि किसी ने Submit click किया या नहीं, और अगर किया तो उन्होंने जो typed किया उसे process करे।
Syntax
markup
def view_name(request):
    if request.method == 'POST':
        form = FormName(request.POST)
        if form.is_valid():
            # use form.cleaned_data
            return redirect('url_name')
    else:
        form = FormName()
    return render(request, 'template.html', {'form': form})

GET/POST Branch

एक view request.method check करता है यह decide करने के लिए कि एक blank form दिखाए या submitted data process करे।

उदाहरण: The GET/POST Branch

A view checks request.method to decide whether to show a blank form or process submitted data.

markup
def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
    else:
        form = ContactForm()
    return render(request, 'contact.html', {'form': form})
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Valid Data Process करना

एक बार POST branch के अंदर is_valid() pass हो जाए, view cleaned_data उपयोग कर सकता है और फिर redirect कर सकता है।

उदाहरण: Processing Valid Data

Once is_valid() passes inside the POST branch, the view can use cleaned_data and then redirect.

markup
from django.shortcuts import render, redirect


def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            return redirect('thank-you')
    else:
        form = ContactForm()
    return render(request, 'contact.html', {'form': form})
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Success के बाद Redirect करना

एक successful POST के बाद redirect करना browser को form resubmit करने से रोकता है अगर user page refresh करे।

Note: इसे Post/Redirect/Get pattern कहा जाता है।

उदाहरण: Redirect After Success

Redirecting after a successful POST prevents the browser from resubmitting the form if the user refreshes the page.

markup
if form.is_valid():
    form.save()
    return redirect('thank-you')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Form data process करने से पहले request.method == POST check न करना, इसलिए GET requests भी एक form validate करने की कोशिश करते हैं।
  2. एक successful POST के बाद redirect करना भूल जाना, जिससे एक page refresh accidentally form resubmit कर दे।
  3. Validation fail होने पर bound form को errors के साथ फिर से render न करना, जो silently user ने जो typed किया उसे discard कर देता है।
चैप्टर सारांश
  • एक single view आमतौर पर दोनों GET (empty form दिखाना) और POST (submitted form process करना) handle करता है।
  • if request.method == POST pattern form दिखाने और process करने के बीच branch करता है।
  • एक successful POST के बाद redirect करें (Post/Redirect/Get pattern) refresh पर duplicate submissions से बचने के लिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.