Handling different HTTP methods in Django views
📖 Scenario: You are building a simple Django web app that responds differently to GET and POST requests on the same URL.This is like a restaurant where the waiter listens to different requests: one to show the menu (GET) and one to take your order (POST).
🎯 Goal: Create a Django view function called handle_request that returns a different HTTP response depending on whether the request method is GET or POST.For GET requests, respond with the text 'This is a GET request'. For POST requests, respond with the text 'This is a POST request'.
📋 What You'll Learn
Create a Django view function named
handle_requestUse the
request.method attribute to check the HTTP methodReturn an
HttpResponse with the exact text 'This is a GET request' for GET requestsReturn an
HttpResponse with the exact text 'This is a POST request' for POST requests💡 Why This Matters
🌍 Real World
Web servers often need to respond differently to GET and POST requests on the same URL, such as showing a form on GET and processing it on POST.
💼 Career
Understanding how to handle different HTTP methods is essential for backend web development and building RESTful APIs.
Progress0 / 4 steps