POST Requests को Handle करना
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.
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.
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 करे।
उदाहरण: Redirect After Success
Redirecting after a successful POST prevents the browser from resubmitting the form if the user refreshes the page.
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. #}
- Form data process करने से पहले
request.method == POSTcheck न करना, इसलिए GET requests भी एक form validate करने की कोशिश करते हैं। - एक successful POST के बाद redirect करना भूल जाना, जिससे एक page refresh accidentally form resubmit कर दे।
- Validation fail होने पर bound form को errors के साथ फिर से render न करना, जो silently user ने जो typed किया उसे discard कर देता है।
- एक single view आमतौर पर दोनों GET (empty form दिखाना) और POST (submitted form process करना) handle करता है।
if request.method == POSTpattern form दिखाने और process करने के बीच branch करता है।- एक successful POST के बाद redirect करें (Post/Redirect/Get pattern) refresh पर duplicate submissions से बचने के लिए।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: