0
0
Djangoframework~30 mins

Named URLs for maintainability in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Named URLs for maintainability
📖 Scenario: You are building a simple Django website for a bookstore. You want to make your URL management easier and more maintainable by using named URLs instead of hardcoding paths everywhere.
🎯 Goal: Create named URLs in Django and use them in templates to link pages. This will help you change URLs easily later without breaking links.
📋 What You'll Learn
Create a URL pattern with a name
Create a simple view function
Use the named URL in a Django template with the {% url %} tag
Ensure the project uses named URLs for linking pages
💡 Why This Matters
🌍 Real World
Web developers use named URLs in Django projects to make changing URLs easier and avoid broken links when site structure changes.
💼 Career
Understanding named URLs is essential for Django developers to write clean, maintainable, and scalable web applications.
Progress0 / 4 steps
1
Create a URL pattern for the home page
In your Django app's urls.py, create a URL pattern for the home page path '' that points to a view function called home_view. Use the exact path '' and view name home_view.
Django
Need a hint?

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

2
Add a name to the URL pattern
Modify the URL pattern for the home page to add the name 'home' using the name argument in the path() function.
Django
Need a hint?

Add name='home' inside the path() call.

3
Create the home_view function
In your views.py, create a function called home_view that takes a request argument and returns a simple HttpResponse with the text 'Welcome to the Bookstore'. Import HttpResponse from django.http.
Django
Need a hint?

Define home_view that returns HttpResponse('Welcome to the Bookstore').

4
Use the named URL in a template
In your Django template file base.html, create a link to the home page using the named URL 'home' with the Django template tag {% url 'home' %}. The link text should be Home. Write the full anchor tag.
Django
Need a hint?

Use <a href="{% url 'home' %}">Home</a> to link using the named URL.