How to get base_url in django template

Given the website, how do you get the HOST of this in a django template without passing this var from the view?

http://google.com/hello --> {{ BASE_URL }} ==> 'http://google.com' 
+8
python django
source share
2 answers

This is described in detail in the next message.

There are several ways to do this:

  • As described by david542 **
  • Using {{request.get_host}} in your template **
  • Using the contrib.sites structure

** Please note that they can be tampered with.

+12
source share

You can get the request object in your template by adding the following TEMPLECT_CONTEXT_PROCESSOR binder to your settings:

 TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.request', ) 

Below is the documentation . Then you can call your template:

 {{ request.META.HTTP_NAME }} 

And that will give you the base url.

0
source share

All Articles