Login

Get the full request path

Author:
limodou
Posted:
February 27, 2007
Language:
Python
Version:
Pre .96
Score:
4 (after 6 ratings)

The result maybe:

http://localhost/test/

And for request.path, it will not include the domain field(http://localhost).

1
2
3
def get_full_path(request):
    full_path = ('http', ('', 's')[request.is_secure()], '://', request.META['HTTP_HOST'], request.path)
    return ''.join(full_path)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 9 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 3 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
  5. Help text hyperlinks by sa2812 1 year, 5 months ago

Comments

robbie (on March 6, 2007):

To be a bit more redundant, you could make that:

def get_full_path(request):
    return 'http' + ('', 's')[request.is_secure()] + '://' + request.META['HTTP_HOST'] + request.path

...or better:

def get_full_path(request):
    full_path = ('http', ('', 's')[request.is_secure()], '://', request.META['HTTP_HOST'], request.path)
    return ''.join(full_path)

#

limodou (on March 6, 2007):

Thanks. I lost secure.

#

noomz (on September 15, 2014):

You can use built-in function build_absolute_url()

#

Please login first before commenting.