Step-by-step guide:
- Check project structure
- Install required libraries
- Create Server.py file
- Edit wsgi.py file
- Edit settings.py file
- Run command for running the server
yourdjangoprojectname is the name of your project. I'm just using it here as a project name.
├── manage.py
├── server.py
├── yourdjangoprojectname
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
pip install waitress
pip install whitenoise
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourdjangoprojectname.settings")
# for using WhiteNoise:
from whitenoise import WhiteNoise
application = WhiteNoise(get_wsgi_application())
from waitress import serve
from yourdjangoprojectname.wsgi import application
if __name__ == '__main__':
serve(application, port='8000')
For WhiteNoise to work we need to add middleware for WhiteNoise.
MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
# ...
]
You can run the project on the Waitress server by simply typing the command.
python server.py