No tension! You can host your web app on render at free of cost for 90 days.

Web hosting

Deploy python django app on render platform at free of cost.


The prerequisites:

  1. A python django app
  2. Python3
  3. Django
  4. Account in Render platform
  5. Package to be installed: gunicorn, whitenoise, dj-database-url, psycopg2-binary
  6. Dbeaver, a desktop app to conect different type of databses

The Django App Structure:

For example we have a django app having the below files and directories structure:

MYAPP/

  ├── app/
  │   ├── migrations/
  │   │   └── __init__.py
  │   ├── __init__.py
  │   ├── admin.py
  │   ├── apps.py
  │   ├── models.py
  │   ├── test.py
  │   └── views.py

  ├── myapp/
  │   ├── __init__.py
  │   ├── settings.py
  │   ├── urls.py
  │   └── wsgi.py

  ├── manage.py

  ├── Pipfile
  └── Pipfile.lock

Steps to get ready to deploy your app:

  1. Sign up to render.com

  2. Create a Requirements File: We can generate a requirements.txt file in the root directory which listing the project dependencies by using the commandline:

# pip freeze > requirements.txt
  1. Configure Django for Production: Update your settings.py file for production. Make sure to set DEBUG to False

  2. Configure static file:

python3 manage.py collectstatic
  This command will look at all the installed apps it will get all their static files and copy to the static folder.
  We will see a message like this in output: 125 static files copied to '/Users/user/myapp/static'.

5. Install gunicorn: The Gunicorn “Green Unicorn” is a Python Web Server Gateway Interface HTTP server. Run:

penv install gunicorn
  1. Create Procfile: Add a new file called “Procfile” at root directory. Note: spell this exactly the same and this is the file render looks at to start the application.
web: gunicorn vidjan.wsgi
  1. Database setup in setting.py module: Make sure the database list seems like this:
DATABASES = {
              'default': {
                  'ENGINE': 'django.db.backends.sqlite3',
                  'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
              }
          }
  1. Create a New Web Service on Render:
  1. Update Host in settings.py: In Django, the ALLOWED_HOSTS setting is a security measure to prevent HTTP Host header attacks. It specifies a list of valid host/domain names for your Django application. When Django receives a request, it checks the Host header against the values in ALLOWED_HOSTS. If the Host header does not match any of the allowed values, Django raises a SuspiciousOperation (400 Bad Request) exception.

So we need to copy the url from the render platform and update the allowed host in settings.py file. The code seems like:

ALLOWED_HOSTS = [myapp.onrender.com]
  1. Then git commit and push. Then we can see the website properbly.

Deploying the django app

Welcome to the documentation guide for deploying a Django application with a PostgreSQL database on the Render platform. This guide will walk you through the process of connecting Render’s PostgreSQL database to your Django app, ensuring a seamless deployment experience.

In this tutorial, you will learn the step-by-step instructions for deploying both the Django application and its associated database on the Render platform. By the end of this guide, you’ll have a fully operational Django app running on Render, with a connected PostgreSQL database.

Please follow the detailed instructions provided in each section to successfully set up and deploy your Django application on Render, utilizing the PostgreSQL database for data storage.

Let’s get started on this journey of deploying and hosting your Django app with Render!

Steps to follow:

DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
                'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
            }
}
*   Install the dj-database-url class and run the command:
pipenv install dj-database-url
import dj_database_url
DATABASES['default'] = dj_database_url.parse('external_url_to_connect_postgresql_that_copied_from_render')
python3 manage.py migrate
pipenv install psycopg2-binary

or it could be: psycopg2 without the binary basde on the local machine.

python3 manage.py runserver
    then add data then again open the Dbeaver and see the data is up here or not.
pip freeze > requirements.txt

This will create the file automatically in the root directory.

Steps to add new data fields in local then migrate to production:

Up to this point, your app is live on render hosting. Now you change or add new data field in your app localy that could updates the local database, qslite3. Then test in local machine if things are okay then push and you need migrate local changes to the production. And the steps of full process as below:

python3 manage.py makemigrations
Note: After the new data filed is added it might not allow you to make migrations because the data filed for privious data become none so you need to provide default data for the empty fields for previous data. When you run the make migrations command it prompts 2 options to provide the default data like below:
    Please select a fix:
    1. Provide a one-off default now (will be set on all existing rows with a null value for this column)
    2. Quit and manually define a default value in models.py.

  Then input the default value for the empty fileds in the terminal as instruction then complete the make migrations.

Then to migrate run:
python3 manage.py migrate
export DATABASE_URL=external_database_url

You can copy the external_database_url from the postgresql database page on render postgresql pages.

python3 manage.py migrate
Khalid

© 2024 Khalid

GitHub 𝕏