How can I get the scheme (http or https) from a Django request object? I'm using Django 1.5, which is before request.scheme was introduced.
You can get the scheme by calling request.scheme
in the view:
def view(request):
scheme = request.scheme
...
Alternatively, you can also check the return value of .is_secure()
method:
def view(request):
scheme = request.is_secure() and "https" or "http"
...
Alternatively, you can use .build_absolute_uri()
to get the absolute URI of the request and parse it using .urlsplit()
to retrieve the scheme:
from django.utils.six.moves.urllib.parse import urlsplit
def view(request):
scheme = urlsplit(request.build_absolute_uri(None)).scheme
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With