Challenge 1: Write a GET/POST Function-Based View — Possible Solution ==================================================================== # catalog/views.py from django.http import HttpResponse def subscribe(request): if request.method == "POST": email = request.POST.get("email", "") if not email: return HttpResponse("Please provide an email address.", status=400) return HttpResponse(f"Thanks — {email} is now subscribed!") # request.method == "GET" form_html = """
""" return HttpResponse(form_html) # catalog/urls.py from django.urls import path from . import views urlpatterns = [ path("subscribe/", views.subscribe, name="subscribe"), ] WHY THIS WORKS -------------- - Branching on request.method inside a single function is the standard FBV pattern for "one URL, two behaviors depending on HTTP verb" — the same route (/subscribe/) shows a form on GET and processes it on POST. - request.POST.get("email", "") reads the form field by its `name` attribute — using .get() with a default avoids a KeyError if the field is somehow missing from the submitted data. - Every branch has an explicit `return HttpResponse(...)` — including the early-return validation check for a missing email — which is exactly the discipline the chapter's gotcha calls out: Django will raise a real error if any code path falls through without returning a proper response object.