Search code examples
pythondjango

How to get getting base_url in django template


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'

Solution

  • This has been answered extensively in the following post

    There are several ways of doing it:

    1. As david542 described **
    2. Using {{ request.get_host }} in your template **
    3. Using the contrib.sites framework

    ** Please note these can be spoofed