I am a newbie in Django and web development. I want to implement 开发者_Python百科 Exactly this Question, but in django. I have searched many blogs and questions, nowhere was i able to find,exactly how to implement this. The SSL Middleware Django, is something i couldnt grasp very well. If that is the solution, can anyone please tell me how to implement it?
Is the question clear ? or do i need to add a few things, please comment i will make the necessary changes.Any help will be highly appreciated.Thanks in advance.
P.S: I have added the ssl certificate on the server. So that is taken care of.
You just need to add the middleware class to the list of middleware in your settings.py and follow the instructions for your views as directed in the snippet. Here's the documentation guide to middleware.
Hope that helps you out.
Here's an example middleware that redirects to the SSL site if HTTP is used:
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',
...
]
精彩评论