Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django, get scheme (http or https), pre request.scheme implementation

Tags:

django

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.

like image 391
user984003 Avatar asked Apr 23 '16 23:04

user984003


1 Answers

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
    ...
like image 94
Ozgur Vatansever Avatar answered Jun 17 '23 08:06

Ozgur Vatansever