How do I get the domain name of my current site from within a Django template? I've tried looking in the tag and filters but nothing there.
+1
A:
I think what you want is to have access to the request context, see RequestContext.
phsiao
2009-09-20 14:35:03
if you mean request.url, you only get the path minus the domain
2009-09-20 14:39:59
`request.META['HTTP_HOST']` gives you the domain. In a template it would be `{{ request.META.HTTP_HOST }}`.
Daniel Roseman
2009-09-20 14:42:01
+1
A:
If you want the actual HTTP Host header, see Daniel Roseman's comment on @Shawn Hsiao's answer. The other alternative is if you're using the contrib.sites framework, you can set a canonical domain name for a Site in the database (mapping the request domain to a settings file with the proper SITE_ID is something you have to do yourself via your webserver setup). In that case you're looking for:
from django.contrib.sites.models import Site
current_site = Site.objects.get_current()
current_site.domain
you'd have to put the current_site object into a template context yourself if you want to use it. If you're using it all over the place, you could package that up in a template context processor.
Carl Meyer
2009-09-21 15:04:16