How to implement Https (SSL Middleware) in Django

I am new to Django and web development. I want to implement It is this question , but in django. I searched a lot of blogs and questions, nowhere I could not find how to implement this. SSL Middleware Django , this is something that I could not understand very well. If this is a solution, can someone tell me how to implement it?

Is the question clear? or I need to add a few things, please comment, I will make the necessary changes. Any help would be greatly appreciated. Thanks in advance.

PS: I added the ssl certificate to the server. So take care of it.

+2
django ssl django-middleware
source share
2 answers

You just need to add the middleware class to the middleware list in the settings.py options and follow the instructions for your views, as indicated in the snippet. Here is the middleware documentation guide .

Hope this helps you.

+1
source

Here's an example of middleware redirecting to an SSL site if using HTTP:

from django.http import HttpRequest from django.shortcuts import redirect #Require SSL com only. If we get anything else, redirect to https / class RequireSSL(object): def process_request(self, request): assert isinstance( request, HttpRequest ) if not request.is_secure(): return redirect( 'https://%s/' % request.get_host() ) 

Then inside your settings.py:

 MIDDLEWARE_CLASSES = [ 'website.middleware.require_ssl.RequireSSL', ... ] 
+1
source

Source: https://habr.com/ru/post/649851/


All Articles