0
0
Djangoframework~30 mins

How Django processes a request (URL → View → Template) - Try It Yourself

Choose your learning style9 modes available
How Django processes a request (URL → View → Template)
📖 Scenario: You are building a simple Django web app that shows a welcome message on the homepage. You want to understand how Django takes a web address (URL), finds the right code to run (view), and then shows a webpage (template) to the user.
🎯 Goal: Create a Django project with a URL pattern, a view function, and a template that work together to display the text "Welcome to Django!" on the homepage.
📋 What You'll Learn
Create a URL pattern for the homepage ('/')
Create a view function called home_view that returns a rendered template
Create a template file called home.html that displays the welcome message
Connect the URL pattern to the home_view function
💡 Why This Matters
🌍 Real World
Websites use this URL → View → Template flow to show pages to users based on the web address they visit.
💼 Career
Understanding this flow is essential for backend web developers working with Django to build dynamic websites.
Progress0 / 4 steps
1
Set up the URL pattern
In the urls.py file, import path from django.urls. Create a list called urlpatterns with one entry: use path to map the empty string '' (homepage) to a view function called home_view. Do not import or define home_view yet.
Django
Need a hint?

Remember, urlpatterns is a list of path calls. The first argument is the URL pattern as a string, the second is the view function name.

2
Create the view function
In the views.py file, import render from django.shortcuts. Define a function called home_view that takes a request parameter. Inside the function, return the result of calling render with request, the template name 'home.html', and no context.
Django
Need a hint?

The view function must accept request and return render(request, 'home.html').

3
Create the template file
Create a file named home.html inside a folder called templates. Inside home.html, write HTML code that displays the text Welcome to Django! inside an <h1> tag.
Django
Need a hint?

The template is a simple HTML file with an <h1> tag containing the welcome text.

4
Connect URL to view and run server
Make sure the home_view function is imported in urls.py from the views module. The urlpatterns list should map the empty string '' to home_view. This completes the connection from URL to view to template.
Django
Need a hint?

Import home_view in urls.py using from .views import home_view.