Hard-coded URL in template

Hard-coding urls makes changing URLs harder, and makes linking to the wrong page easier - so harms maintainability.

Django encourages giving URLs a name so they can be resoved later (rather than hard coding the URL).

This be taken advantage of by defining a `name` in the url.py entry, and then using that name in templates.

So in practice, do this


    # in urls.py
    urlpatterns = [
        path('articles/', Articles.as_view(), name='articles-list'),
    ]

    # in index.html
    <a href="{% url 'articles-list' %}">Go to articles</a>

Instead of this


    # in urls.py
    urlpatterns = [
        path('articles/', Articles.as_view()),
    ]

    # in index.html
    <a href="/articles/">Go to articles</a>

Are you affected by this? Audit your codebase in seconds to find out.

Configuring this check

Django Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code hard-coded-url in your pyproject.toml file.

Read more about configuring Django Doctor.