BeginnerCoder

Lesson 24 — Class-based view dispatch

One class, one method per HTTP verb

Browsers send requests with a method: GET to read a page, POST to send a form. A Django class-based view has a dispatch() that looks at the method and calls get() or post(). If the class has no such method, the answer is 405 Method Not Allowed.

getattr(obj, "get", default) finds a method by name, or returns default if it does not exist.

Note: the exercise runs in your browser without Django installed, so it is a small plain-Python simulation of what Django does for you.

Why it matters: this is exactly how Django's View class works inside — and it keeps read and write logic apart.

Want it another way?
Task

Add a delete() method to Home returning "DELETE home"; PUT should still give 405.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Django Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.