Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you check the internet protocol from Django's template?

Right now, if I want to check whether the current page is accessed through http:// or https://, I will use the following Javascript in my templates and write html from document.write:

<script type="text/javascript">
var protocol = document.location.protocol;
if (protocol == "http:")
    document.write("regular");
else
    document.write("secured");
</script>

Is there another way to achieve the above in Django's template without using Javascript?

like image 527
Thierry Lam Avatar asked Sep 10 '25 02:09

Thierry Lam


2 Answers

if you use a RequestContext, you can do the following:

<p>You used: {% if request.is_secure %}HTTPS{% else %}HTTP{% endif %}

See the relevant part of the Django documentation.

like image 121
jcdyer Avatar answered Sep 12 '25 21:09

jcdyer


Since Django 1.10, you can use:

request.scheme

in a view, or in a template:

{{ request.scheme }}

From the docs

A string representing the scheme of the request (http or https usually).

like image 24
slavugan Avatar answered Sep 12 '25 21:09

slavugan