Error when integrating django channels with existing django 3.1 wsgi code

Viewed 1214

I am getting the following error when adding channels in Django.

Exception inside application: init() takes 1 positional argument When adding ASGI_APPLICATION in Django 3.1 for channels

Exception inside application: __init__() takes 1 positional argument but 2 were given
Traceback (most recent call last):
  File "/home/dell/Desktop/s/s/s-env/lib/python3.6/site-packages/channels/staticfiles.py", line 44, in __call__
    return await self.application(scope, receive, send)
  File "/home/dell/Desktop/s/s/s-env/lib/python3.6/site-packages/channels/routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "/home/dell/Desktop/s/s/s-env/lib/python3.6/site-packages/channels/routing.py", line 160, in __call__
    send,
  File "/home/dell/Desktop/s/s/s-env/lib/python3.6/site-packages/asgiref/compatibility.py", line 33, in new_application
    instance = application(scope)
TypeError: __init__() takes 1 positional argument but 2 were give

The application works fine on WSGI configuration.The above error only appears when ASGI_APPLICATION = 'app.routing.application' is added.

Since it's showing error originating from static files, I have tried generating static files again but that doesn't help.

I am using default django asgi file:

"""
ASGI config for mevsyou project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 's.settings.local')

application = get_asgi_application()

s/routing.py:

from channels.routing import ProtocolTypeRouter, URLRouter
from users import routing

application = ProtocolTypeRouter({
    'http': URLRouter(routing.urlpatterns),
})
1 Answers

Your code is for Channels 2, while you're using Channels 3.

In Channels 3, you should not have routing.py. But your asgi.py file should look like this:

import os

from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
})

As a matter of fact, ASGI_APPLICATION should be set to 'app.asgi.application'.

See Channels docs for 3.x

Related