How does one put a link / url to the web-site's home page in Django?


In Django templates, is there a variable in the context (e.g. {{ BASE\_URL }}, {{ ROOT\_URL }}, or {{ MEDIA\_URL }} that one can use to link to the home url of a project?

I.e. if Django is running in the root of a project, the variable (let's call it R) {{ R }} in a template would be /. If the root url is a sub-folder http://host/X/ the variable {{ R }} would be /X/ (or http://host/X/).

It seems painfully simple, but I can't find an answer. :) Thank you!

9/7/2013 11:44:42 PM

Accepted Answer

You could give the URL configuration which you're using to handle the home page a name and use that:

from django.conf.urls.defaults import *

urlpatterns = patterns('myproject.views',
    url(r'^$', 'index', name='index'),


<a href="{% url index %}">...

UPDATE: Newer versions of Django require quotation marks around the name of the view:

<a href="{% url 'index' %}">...

This note in the Django Book has some tips about deploying your applications to a subdirectory:

2/3/2018 12:58:55 AM

I always use something like <a href="/"> (assuming your home is at the root, of course). I seem to recall looking this up once, and couldn't find a Django variable for this path; at any rate, / seemed pretty easy, anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow