HttpRequest और HttpResponse
def view_name(request):
method = request.method
value = request.GET.get('key', 'default')
return HttpResponse('content')
HttpRequest से Data पढ़ना
हर view में pass हुआ request object useful attributes रखता है: request.method (HTTP verb), request.GET (query string parameters), request.POST (submitted form data), और request.user (current user)।
उदाहरण: Reading Data off HttpRequest
The request object passed into every view carries useful attributes: request.method (the HTTP verb), request.GET (query string parameters), request.POST (submitted form data), and request.user (the current user).
from django.http import HttpResponse
def whoami(request):
method = request.method
name = request.GET.get('name', 'guest')
return HttpResponse(f'{method} request from {name}')
{# 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. #}
एक HttpResponse Return करना
हर view को किसी ना किसी form का HttpResponse return करना चाहिए। सबसे simple case plain text wrap करता है; Django JSON के लिए JsonResponse और redirects के लिए HttpResponseRedirect भी देता है।
उदाहरण: Returning an HttpResponse
Every view must return some form of HttpResponse. The simplest case wraps plain text; Django also provides JsonResponse for JSON and HttpResponseRedirect for redirects.
from django.http import HttpResponse, JsonResponse
def status(request):
return JsonResponse({'status': 'ok'})
{# 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. #}
Request Method जांचना
request.method पर branch करना एक single view को एक form दिखाना (GET) और इसका submission process करना (POST) दोनों अलग तरीके से handle करने देता है।
उदाहरण: Checking the Request Method
Branching on request.method lets a single view handle both displaying a form (GET) and processing its submission (POST) differently.
from django.http import HttpResponse
def submit(request):
if request.method == 'POST':
return HttpResponse('Form submitted!')
return HttpResponse('Please submit the 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. #}
- request.GET से POST data पढ़ने की कोशिश करना, या request.POST से GET data — वे अलग dictionaries हैं।
- यह भूल जाना कि request.GET और request.POST QueryDict objects return करते हैं, plain dicts नहीं (हालांकि वे similarly behave करते हैं)।
- एक view से HttpResponse में wrap किए बिना एक raw string return करना, जो एक TypeError का कारण बनता है।
- हर view एक HttpRequest object receive करता है जो method, GET/POST data, headers, और logged-in user रखता है।
- हर view को एक HttpResponse object (या JsonResponse या redirect जैसा एक subclass) return करना चाहिए।
- request.method बताता है कि request एक GET, POST, या दूसरा HTTP verb था या नहीं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: