views:

695

answers:

4

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!

A: 

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.

mipadi
+5  A: 

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

urls.py:

from django.conf.urls.defaults import *

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

Templates:

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

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

http://www.djangobook.com/en/1.0/chapter20/#cn43

insin
It seems obvious now. :o) Thanks!
Brian M. Hunt
+3  A: 

In your admin, go to "sites" and set the domain.

Pass context_instance=RequestContext(request) to the templates in question.

Now use {{ SITE_URL }} in any of those templates and you're golden.

Chapter 10 of the Django Book has more information than you'll need regading that context processor bit.

ryansholin
+1  A: 
(r'^$', 'django.views.generic.simple.redirect_to', {'url': '/home/'}),

works fine :)

justin jools