Take the 2-minute tour ×

Exactly the same as this question, but there must be a way to do it without the Sites module. That's just silly... I shouldn't need to query my DB to snag the URL!

I want to use it with reverse().

share|improve this question
Just as an aside: The sites module only hits the DB the first time it needs the site name, the result is cached in a module variable (SITE_CACHE) that will stick around until re-compilation of the module or the SiteManager.clear_cache() method is called. See: code.djangoproject.com/svn/django/tags/releases/1.3/django/… –  Colonel Sponsz May 1 '11 at 11:27

8 Answers 8

up vote 169 down vote accepted

Use handy request.build_absolute_uri() method on request, pass it the relative url and it'll give you full one.

share|improve this answer
+1 This function uses the same data that are in my answer. And is definitely a more convenient option. –  Kugel Feb 27 '10 at 12:17
Took me awhile to get back to this, but yep, works beautifully! Thank you. Just a note to others: it's request.build_absolute_uri(). I was looking for what I import I needed, but you just need to use the request object your view already receives. –  Mark Mar 3 '10 at 3:06
What about the url: localhost/home/#/test ? I can see only localhost/home. How can I see the part after sharp? –  sergzach Sep 18 '11 at 19:23
everything after # is not passed to the server, it's browser-only feature –  Dmitry Shevchenko Sep 19 '11 at 9:01
In a template (where you can't give parameters) you can just do this: {{ request.build_absolute_uri }}{{ object.get_absolute_url }} - and heyho, full url. –  Velmont Apr 2 '13 at 13:51

You can also use get_current_site as part of the sites app (from django.contrib.sites.models import get_current_site). It takes a request object, and defaults to the site object you have configured with SITE_ID in settings.py if request is None. Read more in documentation for using the sites framework


from django.contrib.sites.models import get_current_site
request = None
full_url = ''.join(['http://', get_current_site(request).domain, obj.get_absolute_url()])

It isn't as compact/neat as request.build_absolute_url(), but it is usable when request objects are unavailable, and you have a default site url.

share|improve this answer
I believe my question specifically said "without the Sites module". Does this hit the DB? –  Mark Jan 11 '12 at 19:41
The Sites module has been written to cache Site objects using module level caching (i.e. you don't need the cache framework), so the DB should only get hit the first time a Site is retrieved by a web process. If you don't have django.contrib.sites in your INSTALLED_APPS, it won't hit the DB at all, and provide information based on the Request object (see get_current_site) –  Darb Jan 31 '12 at 10:04
Well then you can has a +1, but build_absolute_uri still looks like the easier and cleaner solution. –  Mark Jan 31 '12 at 19:21
Would be nice if there was a Site.build_absolute_uri shortcut, or if build_absolute_uri could take None as the request object... –  Darb Feb 1 '12 at 9:07
Does not work, if you use https. Yeah, you could add the s, but do you develop with https locally? and do you always know, if you have https but not sometimes...? –  omeinusch May 5 at 12:30

If you want to use it with reverse() you can do this : request.build_absolute_uri(reverse('view_name', args=(obj.pk, )))

share|improve this answer

Examine Request.META dictionary that comes in. I think it has server name and server port.

share|improve this answer
use request.META['HTTP_HOST'] –  Antony Oct 13 at 10:16

If you can't get access to request then you can't use get_current_site(request) as recommended in some solutions here. There is a workaround, however:

from django.contrib.sites.models import Site
share|improve this answer
This is really handy when you don't have access to HttpRequest object. e.g. in tasks, signals etc. –  Arsham Nov 11 at 10:04

If you don't want to hit the database, you could do it with a setting. Then, use a context processor to add it to every template:

# settings.py
BASE_URL = 'http://example.com'

# myapp/context_processors.py
from django.conf import settings

def extra_context(request):
    return {'base_url': settings.BASE_URL}

# my_template.html
<p>Base url is {{ base_url }}.</p>
share|improve this answer

To create a complete link to another page from a template, you can use this:

{{ request.META.HTTP_HOST }}{% url 'views.my_view' my_arg %}

request.META.HTTP_HOST gives the host name, and url gives the relative name. The template engine then concatenates them into a complete url.

share|improve this answer

You can try "request.get_full_path()"

share|improve this answer
This doesn't include the domain. –  TAH Mar 28 '13 at 17:33
domain excluded. –  Clayton Mar 12 at 10:18

Your Answer


