Django asgi deployment failing

I currently have a Django ASGI project that runs well locally, but when I try to deploy it on Render I encounter an error.

Below are my build script (build.sh), Procfile, and relevant sections:

My Asgi.py

import django
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application

django.setup()

from chat.routing import websocket_urlpatterns

from hometweet.routing import websocket_urlpatterns
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "root.settings")

django_asgi_app = get_asgi_application()

application = ProtocolTypeRouter(
{
"http": django_asgi_app,
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
),
}
)

Build Script (build.sh):

#!/bin/bash

# Upgrade pip and setuptools
pip install --upgrade pip
pip install --upgrade setuptools

# Install Dependencies
pip install -r requirements.txt

# Run Migrations
python manage.py makemigrations user
python manage.py makemigrations hometweet
python manage.py makemigrations profiles
python manage.py makemigrations groupapp
python manage.py migrate

**Procfile**
gunicorn root.asgi:application -k uvicorn.workers.UvicornWorker. 

Please someone should help me out on how i can deploy it either on render thanks



I've tried various start commands, including:

- web: daphne -u /tmp/daphne.sock yourproject.asgi:application
- web: gunicorn root.wsgi:application
- gunicorn root.wsgi:application
- web: gunicorn root.asgi:application
- gunicorn root.asgi:application
- web: gunicorn root.asgi:application -k uvicorn.workers.UvicornWorker

The last command is currently giving me a server error when attempting to access the page. is there something i need to change or add please.

Hi there,

It’s hard to know exactly what your start command should be without knowing the directory structure of your application.

Assuming your Asgi.py is a directory called root then gunicorn root.Asgi:application should be okay (note the capital A because your py file starts with this). gunicorn root.Asgi:application -k uvicorn.workers.UvicornWorker should also be okay if you want to use Uvicorn workers with your service.

Regards,

Keith
Render Support, UTC+10 :australia:

my Asgi.py is in my project root directory called root. i tried gunicorn root.Asgi:application and i got no module named root.Asgi

Hi there,

It looks like your Asgi.py has a lowercase a, so gunicorn root.asgi:application -k uvicorn.workers.UvicornWorker would work.

You also need to rename the environment variables you use to connect to your database. Add something like DB_ to the front on them. Using PORT as one of them is causing us problems detecting the port your service uses. I’d actually recommend just using the internal URL and making the environment variable DATABASE_URL, see our example here of how you can use this: https://render.com/docs/deploy-django#configure-django-for-postgresql

Regards,

Keith
Render Support, UTC+10 :australia:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.