[PYTHON] Enable Django https in just a few lines

First make sure your Django project is working properly

pip install django-sslserver

settings.py



INSTALLED_APPS = [
    ...
    'sslserver'
]
 ...
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = False
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
 ...

: point_up: Create an SSL certificate

python



openssl genrsa -out foobar.key 2048
openssl req -new -key foobar.key -out foobar.csr
openssl x509 -req -days 365 -in foobar.csr -signkey foobar.key -out foobar.crt

Start the server again

python manage.py runsslserver 0.0.0.0:8000 --certificate /path/to/foobar.crt --key /path/to/foobar.key

That's it: hugging:

Even so, it's a metaphysical https, so let's have the certificate issued in a proper place.

Recommended Posts

Enable Django https in just a few lines
Create a LINE Bot in Django
How to deploy a Django app on heroku in just 5 minutes
Install Django in a pipenv virtual environment
Dynamically specify a ModelChoiceField queryset in Django
Machine learning experience in just a few lines (Part 2). Explain PyCaret in detail. Model building and evaluation analysis.
Implement a Custom User Model in Django
Set up a simple HTTPS server in Python 3
Start Django in a virtual environment with Pipenv
Build a Django environment with Vagrant in 5 minutes
Extract lines containing a specific "string" in Pandas
Configure a module with multiple files in Django
How to create a Rest Api in Django
Until you create a new app in Django
Models in Django
Just a note
Forms in Django
How to reference static files in a Django project