
PYTHON — Web Server for Django Development Gunicorn with Python
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. — Brian Kernighan

PYTHON — Python Walrus Operator Pitfalls
In this lesson, we will guide you through the process of replacing the development web server built into Django with Gunicorn, a proper HTTP server that can handle traffic concurrently and more securely. Gunicorn, a pure Python implementation, is a popular choice for Django.
Step 1: Install Gunicorn
First, ensure you have activated the relevant virtual environment and then install Gunicorn using pip:
python -m pip install gunicornStep 2: Update Requirements
After installing Gunicorn, remember to update the requirements file:
python -m pip freeze > requirements.txtEnsure that the requirements file contains Gunicorn.
Step 3: Modify the Procfile
Next, modify the Procfile to utilize Gunicorn. The Procfile should contain the following configuration:
web: gunicorn portfolio.wsgiReplace portfolio.wsgi with the fully qualified name of the WSGI module in your portfolio management app.
Step 4: Commit and Push Changes to Heroku
Commit and push your changes to Heroku, including the updated requirements file and the modified Procfile.
Step 5: Verify Deployment
After the build is finished, verify if you can still access your Django project on Heroku.
Step 6: Check Gunicorn Logs
Open the Heroku logs and look for the gunicorn command. You'll notice that it runs two workers that can handle HTTP requests independently, thereby increasing the overall throughput of your app. The number of worker processes is configurable through an optional WEB_CONCURRENCY environment variable.
As an exercise, try increasing the number of workers, redeploy your app, and verify the logs.
That’s it! You have successfully replaced Django’s development web server with Gunicorn.
In the next lesson, you’ll learn how to connect your app to a relational database in the Heroku cloud.
By following these steps, you can ensure that your Django project is served by Gunicorn, improving its performance and security.
As you progress with your Django project, it’s important to leverage tools like Gunicorn to optimize its performance and security. This step-by-step guide will help you seamlessly transition from Django’s development server to Gunicorn for improved web server capabilities.

