0
0
Djangoframework~30 mins

Function-based views basics in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Function-based views basics
📖 Scenario: You are building a simple Django web app that shows a welcome message on the homepage.This app uses function-based views to handle web requests.
🎯 Goal: Create a Django function-based view that returns a simple HTTP response with the text "Welcome to my site!" when the homepage is accessed.
📋 What You'll Learn
Create a function-based view named home_view in views.py
The view must return an HttpResponse with the exact text "Welcome to my site!"
Add a URL pattern in urls.py that maps the root URL '' to home_view
Use only function-based views, no class-based views
💡 Why This Matters
🌍 Real World
Function-based views are the simplest way to handle web requests in Django apps, useful for small projects or simple pages.
💼 Career
Understanding function-based views is essential for Django developers to build and maintain web applications.
Progress0 / 4 steps
1
Create the view function
In views.py, import HttpResponse from django.http. Then create a function called home_view that takes a request parameter.
Django
Need a hint?

Use def to define the function and import HttpResponse at the top.

2
Return a simple HTTP response
Inside the home_view function, return an HttpResponse with the text "Welcome to my site!".
Django
Need a hint?

Use return HttpResponse("Welcome to my site!") inside the function.

3
Import the view in urls.py
In urls.py, import the home_view function from views.
Django
Need a hint?

Use from .views import home_view to import the view.

4
Add URL pattern for the homepage
In urls.py, add a URL pattern that maps the root URL '' to the home_view function using path. Make sure to import path from django.urls.
Django
Need a hint?

Use path('', home_view) inside urlpatterns list.