Given a website, how would you get the HOST of that in a django template, without passing that var from the view?
http://google.com/hello --> {{ BASE_URL }} ==> 'http://google.com'
This has been answered extensively in the following post
There are several ways of doing it:
{{ request.get_host }}
in your template **contrib.sites
framework** Please note these can be spoofed
None of these other answers take scheme into account. This is what worked for me:
{{ request.scheme }}://{{ request.get_host }}
URL: google.com/hello
In Template:
{{ request.get_full_path() }}
return /hello
OR
{{ request.get_host() }}
return google.com
In view:
from django.contrib.sites.shortcuts import get_current_site
def home(request):
get_current_site(request)
# google.com
# OR
request.get_host()
# google.com
# OR
request.get_full_path()
# /hello
You can get the request
object in your template by adding in the following TEMPLECT_CONTEXT_PROCESSOR
middleware in your settings:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
)
Here is some documentation on it. Then you can call in your template:
{{ request.META.HTTP_NAME }}
And that will give you the base url.
In your template to get base url
{{ request.get_host() }}