Complete the code to define a Django view that returns a simple HTTP response.
from django.http import HttpResponse def my_view(request): return [1]("Hello, world!")
The HttpResponse class is used to send a simple HTTP response back to the client in Django views.
Complete the code to access the HTTP method used in the request inside a Django view.
def check_method(request): if request.[1] == 'POST': return HttpResponse('Posted!') else: return HttpResponse('Not a POST')
The request.method attribute tells you which HTTP method (GET, POST, etc.) was used.
Fix the error in the view to correctly get a query parameter named 'name' from the request.
def greet(request): name = request.[1].get('name', 'Guest') return HttpResponse(f"Hello, {name}!")
Query parameters are accessed via request.GET in Django views.
Fill both blanks to create a view that renders a template with context data.
from django.shortcuts import [1] def home(request): context = {'title': 'Welcome'} return [2](request, 'home.html', context)
The render shortcut both imports and returns a rendered template with context.
Fill all three blanks to create a view that handles POST data and redirects after processing.
from django.shortcuts import [1], [2] def submit(request): if request.method == 'POST': data = request.POST.get('[3]') # process data here return redirect('success') return render(request, 'submit.html')
We import render to show templates, redirect to send users elsewhere, and get POST data by key name.