How to Start Django Project on Waitress Server (Windows) [Easy-Way]

How to Start Django Project on Waitress Server (Windows) [Easy-Way]

Step-by-step guide:

  1. Check project structure
  2. Install required libraries
  3. Create Server.py file
  4. Edit wsgi.py file
  5. Edit settings.py file
  6. Run command for running the server

yourdjangoprojectname is the name of your project. I'm just using it here as a project name.

Check Project Structure

├── manage.py
├── server.py
├── yourdjangoprojectname
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py

Install Required Libraries

pip install waitress
pip install whitenoise

Edit wsgi.py File

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())

Create server.py File

from waitress import serve
    
from yourdjangoprojectname.wsgi import application
    
if __name__ == '__main__':
    serve(application, port='8000')

Edit settings.py File

For WhiteNoise to work we need to add middleware for WhiteNoise.

MIDDLEWARE = [
    # ...
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",
    # ...
]

Run Command for Running the Server

You can run the project on the Waitress server by simply typing the command.

python server.py

Leave a Reply

Your email address will not be published. Required fields are marked *