508

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

30 Answers 30

691

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'
15
  • 3
    What about the url: localhost/home/#/test ? I can see only localhost/home. How can I see the part after sharp?
    – sergzach
    Commented Sep 18, 2011 at 19:23
  • 59
    everything after # is not passed to the server, it's browser-only feature Commented Sep 19, 2011 at 9:01
  • 83
    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. Commented Apr 2, 2013 at 13:51
  • 28
    And what if I don't have access to request? Like in Django-REST-Framework's Serializers?
    – minder
    Commented Nov 20, 2014 at 21:15
  • 19
    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. Commented Jul 27, 2015 at 18:23
142

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

5
  • 3
    Thanks for the helpful answer. Nothing better than code itself. (also, you probably meant url_name instead of view_name)
    – Anupam
    Commented May 1, 2018 at 10:55
  • 4
    @Anupam reverse() is defined as: def reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None) Commented Jan 31, 2019 at 3:26
  • @ébewè how would one use this in a template?
    – PowerAktar
    Commented Jan 7, 2021 at 18:54
  • 1
    If anyone is interested, you can omit the args=(obj.pk, ) portion if you do not require any parameters. Helped me!
    – Ricky
    Commented Sep 2, 2021 at 23:41
  • 3
    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
    – Dat TT
    Commented Jan 12, 2023 at 15:00
92

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
  • 11
    This is really handy when you don't have access to HttpRequest object. e.g. in tasks, signals etc.
    – Arsham
    Commented Nov 11, 2014 at 10:04
  • 6
    before using this you should enable sites framework docs.djangoproject.com/en/dev/ref/contrib/sites/…
    – madzohan
    Commented Jul 1, 2015 at 15:12
  • 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'.
    – gek
    Commented Nov 7, 2019 at 14:10
  • 1
    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. Commented Jun 7, 2021 at 7:51
  • 1
    @ShalomAlexander How is this not flexible between dev and prod? get_current() takes care of that abstraction for you.
    – shacker
    Commented Jan 17, 2023 at 1:07
78

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
  • 6
    I believe my question specifically said "without the Sites module". Does this hit the DB?
    – mpen
    Commented Jan 11, 2012 at 19:41
  • 3
    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
    Commented Jan 31, 2012 at 10:04
  • 1
    Well then you can has a +1, but build_absolute_uri still looks like the easier and cleaner solution.
    – mpen
    Commented Jan 31, 2012 at 19:21
  • 1
    This is a perfect answer if you are trying to generate URLs in signals to dispatch emails from.
    – Chris
    Commented Jul 8, 2013 at 19:20
  • 3
    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...?
    – tjati
    Commented May 5, 2014 at 12:30
29

In your view, just do this:

base_url =  "{0}://{1}{2}".format(request.scheme, request.get_host(), request.path)
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
  • Good stuff. This combined with a .env better satisfies my use case.
    – alexcs
    Commented Dec 17, 2020 at 12:35
  • 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.
    – Dat TT
    Commented Jan 12, 2023 at 15:18
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
  • @StevenChurch It should work. I haven't marked Django 2.0 as supported yet, but the existing version should work.
    – Flimm
    Commented Aug 6, 2018 at 16:53
  • 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.
    – Kyias
    Commented Aug 9, 2018 at 11:17
  • 1
    @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.
    – Flimm
    Commented Aug 14, 2018 at 12:38
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.

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
  • HttpRequest.build_absolute_uri(request) is equivalent to request.build_absolute_uri() isn't it?
    – mpen
    Commented Jan 16, 2015 at 21:54
11

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

2
11

Try the following code:

{{ request.scheme }}://{{ request.META.HTTP_HOST }}
1
  • That'll just give the domain without the path and query string, no?
    – mpen
    Commented Aug 19, 2016 at 15:36
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
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
  • 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
    – Ryan Amos
    Commented May 22, 2016 at 6:16
  • I forgot to mention this is only available if you're using the REST framework. Good catch, I've updated my answer.
    – JohnG
    Commented May 22, 2016 at 18:31
  • Yes thank you - this works like a charm with django REST framework Commented Dec 17, 2016 at 0:21
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}}
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
  • Thanks for this. Getting request object from URLNode instance request = context['request'] was it
    – Anupam
    Commented Sep 7, 2022 at 11:01
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
  • 1
    For the first suggestion I got: reverse() got an unexpected keyword argument 'request'
    – getup8
    Commented Jul 6, 2021 at 17:10
  • The first one saved me, for some reason build_absolute_uri is not available on ASGIRequest .. when using Django View class. Thanks Commented Mar 25 at 9:51
3

I got it:

wsgiref.util.request_uri(request.META)

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

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') }"
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.

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!

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'))

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
1

request.get_host() will give you the domain.

1
  • 1
    The question states, full URL
    – acidjunk
    Commented Jul 5, 2016 at 17:36
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
  • so basically request.build_absolute_uri will build URL of the current location Commented Nov 14, 2020 at 10:29
1

I use this code :

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

response :

https://yourdomain.com
0
request.get_host()

Use this for request object for APIView in django

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.

-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]
0
-3

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
  • Yeah..you pointed out the problem. Hostname is not necessarily the same as the domain name.
    – mpen
    Commented Apr 19, 2016 at 5:04
  • 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.
    – tbm
    Commented Aug 12, 2016 at 15:32
-5

You can try "request.get_full_path()"

1
  • 4
    This doesn't include the domain.
    – TAH
    Commented Mar 28, 2013 at 17:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.