Deploy a Django App


This is a sample Django app bootstrapped using cookiecutter-django and set up to work with PostgreQL on Render.

The sample app is deployed at https://django.app.render.com.

  1. Create a new PostgreSQL database on Render.

  2. Fork render-examples/django-quick-start and create a new Web Service, giving Render’s GitHub app permission to access your forked repo.

    To make sure Django allows requests on your Render host, we’ve modified the following line in config/settings/production.py:

    # https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
    ALLOWED_HOSTS = env.list('DJANGO_ALLOWED_HOSTS', default=[env('RENDER_APP_HOSTNAME', default='')])

    RENDER_APP_HOSTNAME is the hostname for your Render app when it is first created. If you add a custom domain to your app, make sure to add it to ALLOWED_HOSTS.

  3. Select Python for the environment and use the following values during creation:

    Build Command: ./build.sh

    Here are the contents of the build script:

    #!/usr/bin/env bash
    pip install -r requirements/production.txt
    python manage.py collectstatic
    python manage.py migrate

    It’s simply executing commands needed to build and deploy a Django site on each push to your repo.

    Start Command: gunicorn config.wsgi:application

    It starts Gunicorn and automatically uses the PORT environment variable set by Render.

    Add the following environment variables under the Advanced section:
    KeyValue
    DATABASE_URLThe internal connection string for the database you created above.
    DJANGO_SECRET_KEYA sufficiently strong secret. You can generate a secret locally by running echo "$(openssl rand -base64 32)"
    DJANGO_ADMIN_URLThe output of this command in your terminal: echo $(base64 /dev/urandom | tr -dc 'A-HJ-NP-Za-km-z2-9' | head -c32)/
    DJANGO_SETTINGS_MODULEconfig.settings.production
    WEB_CONCURRENCY4 This will start four Gunicorn workers for your app.

That’s it! Your app will be live on your Render URL as soon as the build finishes.

Once it’s live, create a new Django admin account by running this command in Render’s web shell:

$ ./manage.py createsuperuser

You can also create a new Redis instance on Render and add the REDIS_URL environment variable to use Django’s cache framework

The sample app isn’t configured to use third-party services for external file storage, error reporting or email. You can enable these services by adding the relevant environment variables listed here.