django channels ImproperlyConfigured: Cannot find 'app' in ASGI_APPLICATION module
Asked Answered
M

5

11

I am setting up channels asgi with Django. I have tried upgrading Django and Channels.

"Cannot find %r in ASGI_APPLICATION module %s" % (name, path)
django.core.exceptions.ImproperlyConfigured: Cannot find 'app' in ASGI_APPLICATION module <MyApp>.routing

my routing config is as per the tutorial in mysite/routing

application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
    URLRouter(
        chat.routing.websocket_urlpatterns
    )
  ),
})

and the import statement that is supposed to just be simply

import chat.routing

my directory structure is exactly per the tutorial as well

with setting config

INSTALLED_APPS = [
'channels',
'chat',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

and

ASGI_APPLICATION = 'chat.routing.application'

Thanks

Mcclenaghan answered 5/11, 2018 at 3:51 Comment(1)
Add your routing config. Add relevant code snippets.Nidanidaros
H
10

I got this kind of error when running my Django Channels's routing.py using daphne server.

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

This is what documentation explain about daphne servers,

Daphne is a HTTP, HTTP2 and WebSocket protocol server for ASGI and ASGI-HTTP, developed to power Django Channels.

It supports automatic negotiation of protocols; there’s no need for URL prefixing to determine WebSocket endpoints versus HTTP endpoints.

Note: Daphne 2 is not compatible with Channels 1.x applications, only with Channels 2.x and other ASGI applications. Install a 1.x version of Daphne for Channels 1.x support.

As you can see, we can use both HTTP and WSprotocol through daphne server without using Gunicorn server. What you can do is just add below line to top of your routing.py file.

from .wsgi import *

So now your routing.py file should be like this,

# DockerDjangoNginx is my project name
# your routing.py file should be in this location where the wsgi.py file is placed
# DockerDjangoNginx/DockerDjangoNginx/routing.py

from .wsgi import *  # add this line to top of your code
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import comapp.routing as routing

application = ProtocolTypeRouter({
    # (http->django views is added by default)
    'websocket': AuthMiddlewareStack(
        URLRouter(
            routing.websocket_urlpatterns
        )
    ),
})

Now you can run your daphne server.

(venv) [root@t2mdocker]#daphne -b 0.0.0.0 -p 8000 DockerDjangoNginx.routing:application
2019-05-30 03:33:06,390 INFO     Starting server at tcp:port=8000:interface=0.0.0.0
2019-05-30 03:33:06,391 INFO     HTTP/2 support enabled
2019-05-30 03:33:06,391 INFO     Configuring endpoint tcp:port=8000:interface=0.0.0.0
2019-05-30 03:33:06,392 INFO     Listening on TCP address 0.0.0.0:8000

If you see something like this HTTP/2 support not enabled (install the http2 and tls Twisted extras) when running daphne server, you can run pip install -U Twisted[tls,http2] to correct those errors.

Hypotrachelium answered 30/5, 2019 at 4:2 Comment(2)
Thank you thank you thank you it took days and finally its done only with your guideOrman
@Orman glad to hear that, happy to help you.Hypotrachelium
M
1

Pretty sure this was the problem. Needed to add this asgi.py file next to wsgi.py

import os
import django

from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<myproj>.settings")
django.setup()
application = get_default_application()

and start server with

(vEnv)$daphne <myproj>.asgi:application --port 8888
Mcclenaghan answered 7/11, 2018 at 0:0 Comment(0)
L
1

Change:

ASGI_APPLICATION = 'myproject.routing.application'

To

ASGI_APPLICATION = "myproject.asgi.application"

and issue will hopefully be solved, check the official channels site for more details (https://channels.readthedocs.io/en/stable/installation.html)

Lancelot answered 26/10, 2021 at 7:57 Comment(0)
M
0

Try the following, it worked for me:

from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack

from chat import routing # This change

application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
    URLRouter(
        routing.websocket_urlpatterns   #This change
    )
  ),
})
Matrimony answered 9/8, 2020 at 5:17 Comment(0)
D
0

Check your settings.py, you stated it included:

ASGI_APPLICATION = 'chat.routing.application'

But chat is your added App, it should be the name of the django project itself, so in the tutorial you said you followed it should be something like:

ASGI_APPLICATION = 'mysite.routing.application'
# or
ASGI_APPLICATION = 'core.routing.application'
Diorama answered 22/9, 2021 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.