[Solved]-How do you get the URI from request object in Django?

18👍

request.META['REQUEST_URI']

or

request.get_full_path()

You tend to generate a flood of trivial questions, answers to which you could easily find in documentation/Google…

0👍

In django 3.2 there is no request.META['REQUEST_URI'] as it is in answer above.

Easiest way is to print Meta data, and find what you need:

print(request.META)

This is how i solved this problem:

For example requested url will be:

https://example.com/first_folder/nice_page/

Get domain(example.com):

domain = request.META['HTTP_HOST']
# its will output:
# example.com 

Get path/first_folder/nice_page/:

path = request.META['PATH_INFO']
# or 
path = request.get_full_path()
# both of this options will have the same output:
# /first_folder/nice_page/

Get protocolhttp:// or https://

protocol = request.META['wsgi.url_scheme']
# this will output:
# http   or   https

So your full path may be like this:

protocol = request.META['wsgi.url_scheme']    
domain = request.META['HTTP_HOST']
path = request.META['PATH_INFO']

full_path = protocol + "://" + domain + path

# it's output will be:
# https://example.com/first_folder/nice_page/

Leave a comment