How to implement Https(SSL Middleware) in Django

て烟熏妆下的殇ゞ 提交于 2019-12-06 05:55:20

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