← Back to Django Course | Chapter 2: Apps, URLs & Views | Lesson 9 of 10

HttpRequest और HttpResponse

HttpRequest वह letter है जो एक visitor आपकी website को भेजता है, और HttpResponse वह reply letter है जो आपकी website वापस भेजती है।
Syntax
markup
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).

markup
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.

markup
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.

markup
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. #}
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. request.GET से POST data पढ़ने की कोशिश करना, या request.POST से GET data — वे अलग dictionaries हैं।
  2. यह भूल जाना कि request.GET और request.POST QueryDict objects return करते हैं, plain dicts नहीं (हालांकि वे similarly behave करते हैं)।
  3. एक 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 था या नहीं।

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.