How to implement Https(SSL Middleware) in Django

不打扰是莪最后的温柔 提交于 2019-12-22 18:13:51

问题


I am a newbie in Django and web development. I want to implement 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.


回答1:


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.




回答2:


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',
    ...
]


来源:https://stackoverflow.com/questions/7566112/how-to-implement-httpsssl-middleware-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!