Sitemap
Press enter or click to view image in full size

Django Polls Simplified — URLS

2 min readFeb 5, 2025

--

This is a continuation of:

First things first, Removing hardcoded URLs in templates:

Remember, when we wrote the link to a question in the polls/index.html template, the link was partially hardcoded like this:

<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

The problem with this hardcoded, tightly-coupled approach is that it becomes challenging to change URLs on projects with a lot of templates.

However, since you defined the name argument in the path() functions in the polls.urls module, you can remove a reliance on specific URL paths defined in your url configurations by using the {% url %} template tag:

<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

Namespacing URL names

The tutorial project has just one app, polls.

In real-life, there might be five, ten, twenty apps or more. How does Django differentiate the URL names between them? For example…

--

--

Chris Achinga
Chris Achinga

Written by Chris Achinga

Software Engineer (Python & Javascript)

No responses yet