Handling get and post Methods
Route HTTP verbs to class methods.
Handling get and post Methods is a free Django Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Verbs Become Methods
In a CBV, each HTTP verb maps to a method. A GET request runs your get method; a POST request runs your post method.
Subclass the Base View
For full control over verbs, subclass the base View class. You then define exactly the methods you want to support.
class ContactView(View):
passDefine a get Method
Add a get method to show the page, for example an empty contact form. It takes self and request and returns a response.
def get(self, request):
return render(request, "contact.html")Define a post Method
Add a post method to handle submitted data. Django routes the POST request here automatically.
def post(self, request):
name = request.POST.get("name")
return redirect("thanks")No More method Checks
You no longer write if request.method == "POST". Django dispatches to the right method based on the verb for you.
dispatch Routes the Request
Behind the scenes, a method called dispatch inspects the verb and calls the matching method. You rarely touch it directly.
Unsupported Verbs Get 405
If a request uses a verb you did not define, Django returns 405 Method Not Allowed automatically. Safe by default.
The Classic GET-then-POST Flow
The usual pattern: get shows a form, the user submits, and post processes it. Two methods, one clean class.
Redirect After POST
After a successful post, return a redirect instead of rendering. This stops duplicate submissions on refresh.
Share State via self
Both methods live in the same class, so you can store shared values on self or call helper methods you define.
Wire It With as_view
Route the class the usual way with as_view(). Django builds the dispatcher and your get and post just work.
path("contact/", ContactView.as_view())Quick Check
What happens on a POST to a CBV that only defines get?
Recap: get and post
You saw how CBVs map verbs to methods: get shows, post handles, dispatch routes, and undefined verbs return 405. No manual method checks. 🚦
Frequently asked questions
Is the “Handling get and post Methods” lesson free?
Yes — the full text of “Handling get and post Methods” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Handling get and post Methods”?
Route HTTP verbs to class methods. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Handling get and post Methods” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- From Function Views to View Classes
- TemplateView and get_context_data
- Handling get and post Methods
- Mixins and as_view()