515

How can I get the full/absolute URL (e.g. https://example.com/some/path) in Django 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().

1
  • 18
    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/… Commented May 1, 2011 at 11:27

30 Answers 30

702

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

By default, the absolute URL for request.get_full_path() is returned, but you can pass it a relative URL as the first argument to convert it to an absolute URL.

>>> request.build_absolute_uri() 'https://example.com/music/bands/the_beatles/?print=true' >>> request.build_absolute_uri('/bands/?print=true') 'https://example.com/bands/?print=true' 
Sign up to request clarification or add additional context in comments.

15 Comments

What about the url: localhost/home/#/test ? I can see only localhost/home. How can I see the part after sharp?
everything after # is not passed to the server, it's browser-only feature
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.
And what if I don't have access to request? Like in Django-REST-Framework's Serializers?
I had to use {% if request.is_secure %}https://{% else %}http://{% endif %}{{ request.get_host }}{{ object.get_absolute_url }} because {{ request.build_absolute_uri }} had a trailing slash and {{ object.get_absolute_url }} started with a slash resulting in double slashes in the URL.
|
147

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

6 Comments

Thanks for the helpful answer. Nothing better than code itself. (also, you probably meant url_name instead of view_name)
@Anupam reverse() is defined as: def reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)
@ébewè how would one use this in a template?
If anyone is interested, you can omit the args=(obj.pk, ) portion if you do not require any parameters. Helped me!
This works well for building full url when you have the request object(e.g on a view) and the url is for get request so that redirect http -> https is still fine. This will not work when the request object is not available. For example, to construct the url in scheduled task or asynced task. And the url to hanle post request and the frontend is using https while the django handles request via a proxy
|
97

If you can't get access to request then you can't use get_current_site(request) as recommended in some solutions here. You can use a combination of the native Sites framework and get_absolute_url instead. Set up at least one Site in the admin, make sure your model has a get_absolute_url() method, then:

>>> from django.contrib.sites.models import Site >>> domain = Site.objects.get_current().domain >>> obj = MyModel.objects.get(id=3) >>> path = obj.get_absolute_url() >>> url = f'https://{domain}{path}' >>> print(url) 'http://example.com/mymodel/objects/3/' 

https://docs.djangoproject.com/en/dev/ref/contrib/sites/#getting-the-current-domain-for-full-urls

7 Comments

This is really handy when you don't have access to HttpRequest object. e.g. in tasks, signals etc.
before using this you should enable sites framework docs.djangoproject.com/en/dev/ref/contrib/sites/…
To change example.com to something also: Site.objects.all()[0] returns 'example.com' and has id=1, which specified in settings.py. Just do Site.objects.create(name='production', domain='prodsite.com') and set SITE_ID=2 in settings.py. Now Site.objects.get_current().domain returns 'prodsite.com'.
My development domain is at "127.0.0.1:8000" and the production domain is 'paidfor.pythonanywhere.com'. I want my Django to find own its own which domain is it running on.
@ShalomAlexander How is this not flexible between dev and prod? get_current() takes care of that abstraction for you.
|
79

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

e.g.

from django.contrib.sites.shortcuts 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.

11 Comments

I believe my question specifically said "without the Sites module". Does this hit the DB?
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)
Well then you can has a +1, but build_absolute_uri still looks like the easier and cleaner solution.
This is a perfect answer if you are trying to generate URLs in signals to dispatch emails from.
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...?
|
29

In your view, just do this:

base_url = "{0}://{1}{2}".format(request.scheme, request.get_host(), request.path) 

Comments

27

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 (Django < 1.9) ... BASE_URL = 'http://example.com' TEMPLATE_CONTEXT_PROCESSORS = ( ... 'myapp.context_processors.extra_context', ) # settings.py (Django >= 1.9) TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', # Additional 'myapp.context_processors.extra_context', ], }, }, ] # 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> 

2 Comments

Good stuff. This combined with a .env better satisfies my use case.
Yes, this is the best solution with environment variable, especially for large team and multi environment(dev, staging, uat, production,etc) and de-coupled backend and frontend. The down side is to maintain the .env file for each environments.
21

django-fullurl

If you're trying to do this in a Django template, I've released a tiny PyPI package django-fullurl to let you replace url and static template tags with fullurl and fullstatic, like this:

{% load fullurl %} Absolute URL is: {% fullurl "foo:bar" %} Another absolute URL is: {% fullstatic "kitten.jpg" %} 

These badges should hopefully stay up-to-date automatically:

PyPI

In a view, you can of course use request.build_absolute_uri instead.

3 Comments

@StevenChurch It should work. I haven't marked Django 2.0 as supported yet, but the existing version should work.
For my needs I got round this by passing a ENV from Heroku for failback. My issue is getting the URL to pass through to email templates. I can't remember the problem but it did not work due to a Django change.
@StevenChurch I think the issue when creating emails is that there is no request object to get the domain name from. In that case, you should use the sites framework instead, which gets the domain name from the database. See django-absoluteuri, mentioned in the "see also" section of the README of this PyPI package.
20

This worked for me in my template:

{{ request.scheme }}://{{ request.META.HTTP_HOST }}{% url 'equipos:marca_filter' %} 

I needed the full url to pass it to a js fetch function. I hope this help you.

Comments

13

Yet another way. You could use build_absolute_uri() in your view.py and pass it to the template.

view.py

def index(request): baseurl = request.build_absolute_uri() return render_to_response('your-template.html', { 'baseurl': baseurl }) 

your-template.html

{{ baseurl }} 

1 Comment

HttpRequest.build_absolute_uri(request) is equivalent to request.build_absolute_uri() isn't it?
11

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

2 Comments

use request.META['HTTP_HOST']
The request object has a host on it. Don't examine meta directly: docs.djangoproject.com/en/1.8/ref/request-response/…
11

Try the following code:

{{ request.scheme }}://{{ request.META.HTTP_HOST }} 

1 Comment

That'll just give the domain without the path and query string, no?
10

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.

2 Comments

The answer is missing the protocol (http in this context) and :// part of the URL, so it won't provide a complete url.
The request object has a host on it. Don't examine meta directly: docs.djangoproject.com/en/1.8/ref/request-response/…
7

If you're using django REST framework, you can use the reverse function from rest_framework.reverse. This has the same behavior as django.core.urlresolvers.reverse, except that it uses a request parameter to build a full URL.

from rest_framework.reverse import reverse # returns the full url url = reverse('view_name', args=(obj.pk,), request=request) # returns only the relative url url = reverse('view_name', args=(obj.pk,)) 

Edited to mention availability only in REST framework

3 Comments

I get an error using request=request. It also doesn't seem like request is documented here docs.djangoproject.com/en/1.9/ref/urlresolvers/#reverse
I forgot to mention this is only available if you're using the REST framework. Good catch, I've updated my answer.
Yes thank you - this works like a charm with django REST framework
6

I know this is an old question. But I think people still run into this a lot.

There are a couple of libraries out there that supplement the default Django functionality. I have tried a few. I like the following library when reverse referencing absolute urls:

https://github.com/fusionbox/django-absoluteuri

Another one I like because you can easily put together a domain, protocol and path is:

https://github.com/RRMoelker/django-full-url

This library allows you to simply write what you want in your template, e.g.:

{{url_parts.domain}} 

Comments

5

If anyone is interested in fetching the absolute reverse url with parameters in a template , the cleanest way is to create your own absolute version of the {% url %} template tag by extending and using existing default code.

Here is my code:

from django import template from django.template.defaulttags import URLNode, url register = template.Library() class AbsURLNode(URLNode): def __init__(self, view_name, args, kwargs, asvar): super().__init__(view_name, args, kwargs, asvar) def render(self, context): url = super().render(context) request = context['request'] return request.build_absolute_uri(url) @register.tag def abs_url(parser, token): urlNode = url(parser, token) return AbsURLNode( urlNode.view_name, urlNode.args, urlNode.kwargs, urlNode.asvar ) 

Usage in templates:

{% load wherever_your_stored_this_tag_file %} {% abs_url 'view_name' parameter %} 

will render(example):

http://example.com/view_name/parameter/ 

instead of

/view_name/parameter/ 

1 Comment

Thanks for this. Getting request object from URLNode instance request = context['request'] was it
4

You can either pass request reverse('view-name', request=request) or enclose reverse() with build_absolute_uri request.build_absolute_uri(reverse('view-name'))

2 Comments

For the first suggestion I got: reverse() got an unexpected keyword argument 'request'
The first one saved me, for some reason build_absolute_uri is not available on ASGIRequest .. when using Django View class. Thanks
3

I got it:

wsgiref.util.request_uri(request.META) 

Get the full uri with schema, host, port path and query.

Comments

3

Not for absolute url but I was looking just to get host. If you want to get host in your view.py you can do

def my_view(request): host = f"{ request.scheme }://{ request.META.get('HTTP_HOST') }" 

Comments

2

There is also ABSOLUTE_URL_OVERRIDES available as a setting

https://docs.djangoproject.com/en/2.1/ref/settings/#absolute-url-overrides

But that overrides get_absolute_url(), which may not be desirable.

Instead of installing sites framework just for this or doing some of the other stuff mentioned here that relies on request object, I think the better solution is to place this in models.py

Define BASE_URL in settings.py, then import it into models.py and make an abstract class (or add it to one you're already using) that defines get_truly_absolute_url(). It could be as simple as:

def get_truly_absolute_url(self): return BASE_URL + self.get_absolute_url() 

Subclass it and now you can use it everywhere.

Comments

2

As mentioned in other answers, request.build_absolute_uri() is perfect if you have access to request, and sites framework is great as long as different URLs point to different databases.

However, my use case was slightly different. My staging server and the production server access the same database, but get_current_site both returned the first site in the database. To resolve this, you have to use some kind of environment variable. You can either use 1) an environment variable (something like os.environ.get('SITE_URL', 'localhost:8000')) or 2) different SITE_IDs for different servers AND different settings.py.

Hopefully someone will find this useful!

Comments

2

I came across this thread because I was looking to build an absolute URI for a success page. request.build_absolute_uri() gave me a URI for my current view but to get the URI for my success view I used the following....

request.build_absolute_uri(reverse('success_view_name'))

Comments

2

While working on a project I came to know to get the full/absolute URL in Django.

If your URL looks like this in the address bar:

https://stackoverflow.com/questions/2345708 

And if you want to show the above URL to your template.

  1. {{ request.path }} #Without GET parameters.
  2. {{ request.get_full_path }} #with GET parameters

For the above two codes, this will print in your template will be

questions/2345708 

and another way to get a full URL is:

  • {{request.build_absolute_uri}}

this will print in your template will be:

https://stackoverflow.com/questions/2345708 

Comments

1

request.get_host() will give you the domain.

1 Comment

The question states, full URL
1
<div class='col-12 col-md-6'> <p class='lead'>Login</p> {% include 'accounts/snippets/form.html' with form=login_form next_url=request.build_absolute_uri %} </div> 

Here for example am saying load the form and tell the form that the next URL is the current URL which this code rendred from

1 Comment

so basically request.build_absolute_uri will build URL of the current location
1

I use this code :

request.build_absolute_uri('/')[:-1] 

response :

https://yourdomain.com 

Comments

0
request.get_host() 

Use this for request object for APIView in django

Comments

0

If request is around, there are good answers. There's also good answers on how to obtain it in the template. Here a simple option for accessing the url in the backend.

in settings.py:

ABSOLUTE_URL_BASE = "http://yourdomain.com" 

in models.py or wherever:

from django.conf import settings from django.urls import reverse url = settings.ABSOLUTE_URL_BASE + reverse("app:name") 

One could also use this set-up to create a templatetag.

Comments

-2
class WalletViewSet(mixins.ListModelMixin, GenericViewSet): serializer_class = WalletSerializers pagination_class = CustomPaginationInvestment def get_queryset(self): ###################################################### print(self.request.build_absolute_uri()) ##################################################### wallet, created = Wallet.objects.get_or_create(owner=self.request.user) return Wallet.objects.filter(id=wallet.id) 

You get output like this

http://localhost:8000/v1/wallet HTTP GET /v1/wallet 200 [0.03, 127.0.0.1:41608] 

Comments

-4

You can also use:

import socket socket.gethostname() 

This is working fine for me,

I'm not entirely sure how it works. I believe this is a bit more low level and will return your server hostname, which might be different than the hostname used by your user to get to your page.

2 Comments

Yeah..you pointed out the problem. Hostname is not necessarily the same as the domain name.
This solves a very different problem. Consider a shared hosting server with multiple websites - using the code above, all sites generating URLs will have all such URLs pointing to the host machine, which is likely NOT any of the running websites.
-5

You can try "request.get_full_path()"

1 Comment

This doesn't include the domain.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.