Writing a Function-Based View
Create a view that returns a response.
Writing a Function-Based View is a free Django Academy lesson on CoddyKit — lesson 1 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.
What a View Really Is
A Django view is just a Python function that takes a request and returns a response. That is the whole contract. 🙂
The First Argument: request
Every view's first parameter is the request object. Django hands it to you automatically when a URL matches your view.
Importing HttpResponse
To send something back, you return an HttpResponse. Import it from django.http before you use it.
from django.http import HttpResponseYour First View
Here is a complete, working view. It takes request and returns plain text in a response.
from django.http import HttpResponse
def home(request):
return HttpResponse('Hello, Django!')Where Views Live
By convention, views go in your app's views.py file. Keeping them there makes them easy to find and wire up later.
You Must Return Something
A view that returns None raises an error. Django expects an HttpResponse every single time, with no exceptions.
Returning HTML
The response body is just a string, so you can put HTML inside it. The browser renders it like any page.
def home(request):
return HttpResponse('<h1>Welcome</h1>')Views Are Plain Functions
A view is normal Python, so you can run logic inside it: build a message, check a condition, then return your response.
def greet(request):
name = 'friend'
return HttpResponse('Hi ' + name)Naming Your View
Name views after what they do, like home or about. Clear names make your URL file readable when you connect them.
Function-Based vs Class-Based
This style is a function-based view, the simplest kind. Class-based views come later; functions are perfect to start with.
Nothing Runs Until It Is Mapped
A view does nothing on its own. It only runs when a URL points to it, which is the very next step you will learn.
Quick Check
Let's confirm the core contract of a Django view.
Recap: You Wrote a View
A view is a function that takes request and returns an HttpResponse. You import it, write your logic, and live in views.py. 🎉
Frequently asked questions
Is the “Writing a Function-Based View” lesson free?
Yes — the full text of “Writing a Function-Based View” 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 “Writing a Function-Based View”?
Create a view that returns a response. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing a Function-Based View” 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
- Writing a Function-Based View
- URL Patterns with path()
- Capturing URL Parameters
- Including App URLs and Naming Routes