Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get full url from django request

Tags:

python

django

Is there a better way to get the full url in django than doing the following:

url = request.META['HTTP_HOST']     + request.META['PATH_INFO']     + request.META['QUERY_STRING'] 

Is there something like request.META['URL'] ?

like image 402
David542 Avatar asked Nov 18 '14 07:11

David542


People also ask

How do I reference a URL in Django?

Django offers a way to name urls so it's easy to reference them in view methods and templates. The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py .

How do I pass URL parameters in Django?

Django URL pass parameter to view You can pass a URL parameter from the URL to a view using a path converter. Then “products” will be the URL endpoint. A path converter defines which type of data will a parameter store. You can compare path converters with data types.

What does form {% URL %} do?

{% url 'contact-form' %} is a way to add a link to another one of your pages in the template. url tells the template to look in the URLs.py file. The thing in the quotes to the right, in this case contact-form , tells the template to look for something with name=contact-form .


1 Answers

You can get full URL using request.build_absolute_uri method:

FULL_URL_WITH_QUERY_STRING: request.build_absolute_uri() FULL_URL: request.build_absolute_uri('?') ABSOLUTE_ROOT: request.build_absolute_uri('/')[:-1].strip("/") ABSOLUTE_ROOT_URL: request.build_absolute_uri('/').strip("/") 

Should this will help full to you.

The best way to use ABSOLUTE URLS in Django, you can create a context_processors or middleware and find your ABSOLUTE_URL and return that so that you can use any where in Django.

Like this example:

def absolute(request):     urls = {         'ABSOLUTE_ROOT': request.build_absolute_uri('/')[:-1].strip("/"),         'ABSOLUTE_ROOT_URL': request.build_absolute_uri('/').strip("/"),     }      return urls 

And Then you should use {{ABSOLUTE_ROOT}} in any where into you django template.

like image 124
Yogesh dwivedi Geitpl Avatar answered Oct 07 '22 22:10

Yogesh dwivedi Geitpl