17👍
In my case it was wrong import in different file.
What you should do is
python manage.py shell
from mysite.routing import application
Look what exact error it produces and try to fix that
4👍
Just change
ASGI_APPLICATION = mysite.routing.application
to
ASGI_APPLICATION = “routing.application”
- [Django]-Django: Reference to an outer query may only be used in a subquery
- [Django]-Django simple_tag and setting context variables
- [Django]-What is the Simplest Possible Payment Gateway to Implement? (using Django)
4👍
Check for any potential errors (maybe import error) in consumers.py.
Also, try to put channels as the first item in INSTALLED_APPS in settings.py.
As stated in channels document:
The Channels development server will conflict with any other third-party apps that require an overloaded or replacement runserver command. An example of such a conflict is with whitenoise.runserver_nostatic from whitenoise. In order to solve such issues, try moving channels to the top of your INSTALLED_APPS or remove the offending app altogether.
- [Django]-Playframework and Django
- [Django]-How to implement breadcrumbs in a Django template?
- [Django]-How can I use Django permissions without defining a content type or model?
3👍
This helped me:
Everything with ASGI_APPLICATION
was fine (thanks to all the previous answers given here). But I read logs a bit further and it appeared, there was an import error somewhere else:
ImportError: cannot import name 'channel_session_user_from_http' from 'channels.auth'
Once I got rid of it, everything came back on track.
- [Django]-How to concatenate strings in django templates?
- [Django]-How to translate a model label in Django Admin?
- [Django]-Django edit user profile
2👍
You need to put your routing.py
file inside mayapp/mayapp/routing.py
instead of mayapp/routing.py
- [Django]-Django object multiple exclude()
- [Django]-Unresolved attribute reference 'objects' for class '' in PyCharm
- [Django]-Is there a way to pass a variable to an 'extended' template in Django?
2👍
In case anybody comes along this. Remember: ASGI_APPLICATION = “myapp.routing.application” should go at the bottom of settings.py to ensure nothing get snagged in production!
mysite/myapp/routing.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import myapp.routing
application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
URLRouter(
myapp.routing.websocket_urlpatterns
)
),
})
myapp/routing.py
from django.urls import path
from . import consumers
websocket_urlpatterns = [
path('chatroompage', consumers.ChatConsumer),
]
- [Django]-How to add a new field to a model with new Django migrations?
- [Django]-How to get the common name for a pytz timezone eg. EST/EDT for America/New_York
- [Django]-How can I disable Django's admin in a deployed project, but keep it for local development?
2👍
Django 2.2 doesn’t have inbuilt ASGI support so we need to use Channel’s fallback alternative. Create myproject/asgi.py like this:
import os
import django
from channels.http import AsgiHandler
from channels.routing import ProtocolTypeRouter
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
application = ProtocolTypeRouter({
"http": AsgiHandler(),
# Just HTTP for now. (We can add other protocols later.)
})
source: Channels Docs.
- [Django]-Django-way for building a "News Feed" / "Status update" / "Activity Stream"
- [Django]-Heroku Database Settings Injection – How do I setup my dev django database?
- [Django]-Django-rest-framework + django-polymorphic ModelSerialization
1👍
in my case there were unresolved packages in consumer.py check if you have any unresolved packages in your channels .py files
- [Django]-(13: Permission denied) while connecting to upstream:[nginx]
- [Django]-Determine complete Django url configuration
- [Django]-Django REST Framework ModelSerializer get_or_create functionality
1👍
I am also facing the same problem.
Make sure:
=> The routing.py file should be inside the project root folder(i.e. django server, one that has settings.py, wsgi.py,..)
=> In settings.py include, ASGI_APPLICATION = “yourprojectrootname.routing.application”
=>Sometimes, the coding inside routing.py file may generate this error, to check if this is the case remove all the coding and enter the general template coding,
from channels.routing import ProtocolTypeRouter
application = ProtocolTypeRouter({
# Empty for now (http->django views is added by default)
})
then run the ‘python manage.py runserver’. This time if you didn’t get any error then problem lies in coding inside routing.py. Debug and fix the issue.
=>In other cases this may be of version problem, downgrading to channels==2.1.2 works
- [Django]-Django ModelForm override widget
- [Django]-ValueError: Dependency on app with no migrations: customuser
- [Django]-Add rich text format functionality to django TextField
1👍
When importing your app with absolute path in myapp/routing make sure it is imported like:
import myapp.routing
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import myapp.routing
application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
URLRouter(
myapp.routing.websocket_urlpatterns
)
),
})
It may give you warning as error in PyCharm but you will have working Django channels. I reported it as bug to PyCharm. I hope no one else will spend 3-4 hours on it like me)
- [Django]-Playframework and Django
- [Django]-Integrate django password validators with django rest framework validate_password
- [Django]-SyntaxError: Generator expression must be parenthezised / python manage.py migrate
1👍
I have written the below code in asgi.py file, websocket_urlpatterns is imported from routing.py file. This is worked for me.
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter,URLRouter
from channels.auth import AuthMiddlewareStack
from .routing import websocket_urlpatterns
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myApp.settings')
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
})
- [Django]-How can I tell the Django ORM to reverse the order of query results?
- [Django]-Manager isn't accessible via model instances
- [Django]-Problems trying to format currency with Python (Django)
1👍
try
python3 manage.py shell
>>> from your_project import asgi
if you see below
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/your_project/asgi.py", line 14, in <module>
from django.core.asgi import get_asgi_application
ModuleNotFoundError: No module named 'django.core.asgi'
your django version is lower than 3.x. asgi is supported after 3.x
found it from here
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-What is related_name used for?
- [Django]-Best practice for Python & Django constants
0👍
I was also having the same issue i did everything to resolve it, creating another virtual environment and installing older version of Django but after 2 days of hardware i came to realize that my consumers.py file was missing with just a ‘s’ in consumer’s’ and after that i also corrected in my routing.py file. May be this could be your problem also please do check all the file names first.
- [Django]-What does on_delete=models.PROTECT and on_delete=models.CASCADE do on Django models?
- [Django]-How do you develop against OpenID locally
- [Django]-How to get GET request values in Django Templates?
0👍
I’ve recently faced to this problem, and quickly solved it.
After checking the configurations in settings.py
and routing.py
, I found the problem is in this line:
from channels.auth import AuthMiddlewareStack
the problem is the version compatibility. Then, I upgraded the requirements.txt
to the below and it works quiet well.
channels==2.4.0
channels-redis==2.4.2
daphne==2.5.0
- [Django]-Customizing an Admin form in Django while also using autodiscover
- [Django]-Load a Django template tag library for all views by default
- [Django]-Django: How can I create a multiple select form?
0👍
You should put routing.py
inside mysite/mysite/
not mysite/
otherwise you won’t be able to use ASGI_APPLICATION = mysite.routing.application
in the settings file and you get that error.
- [Django]-How to get the url path of a view function in django
- [Django]-Django-AttributeError 'User' object has no attribute 'backend' (But….it does?)
- [Django]-HTTPError 403 (Forbidden) with Django and python-social-auth connecting to Google with OAuth2
0👍
This problem is course by not importing the get_asgi_application in asgi.py file in settings.py actually had the same issue when i was trying to deploy my app but was able to resolve it by importting get_asgi_application and using it as a traditional http request fallback like so in the asgi.py file :
mport 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
from django.urls import path
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()
from chat.consumers import AdminChatConsumer, PublicChatConsumer
application = ProtocolTypeRouter({
# Django's ASGI application to handle traditional HTTP requests
"http": django_asgi_app,
# WebSocket chat handler
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter([
path("chat/admin/", AdminChatConsumer.as_asgi()),
path("chat/", PublicChatConsumer.as_asgi()),
])
)
),
})
- [Django]-How to correct this error: "'Adminsite' object has no attribute 'root'"
- [Django]-Django add extra field to a ModelForm generated from a Model
- [Django]-Restricting access to private file downloads in Django
-1👍
I solved my problem by:
- python manage.py migrate
- python manage.py makemigrations
- python manage.py migrate
Also check if you have put the routing.py file in the wrong directory. It should be ‘myproject/routing.py’
- [Django]-Add a custom button to a Django application's admin page
- [Django]-IOS app with Django
- [Django]-Django {% with %} tags within {% if %} {% else %} tags?
-2👍
Changing define ASGI_APPLICATION variable and CHANNEL_LAYERS.default.ROUTING variable
from
<project_name>.routing.application
to
routing.application
With this, I can run properly
- [Django]-Pipfile.lock out of date
- [Django]-Running django tutorial tests fail – No module named polls.tests
- [Django]-How to import csv data into django models
-2👍
Django Version problem
None of the above and other stack overflow answers caused a problem to me I tried the full code from documentation using Django version 3 but without no help and the error still appearing every-time
Solving
solved by downgrading Django version from version 3 to 2.2 and followed the instruction of the 2.2 version (notes section) in the documentation
https://channels.readthedocs.io/en/stable/tutorial/part_1.html
- [Django]-How to return static files passing through a view in django?
- [Django]-How to get two random records with Django
- [Django]-Difference between Django's filter() and get() methods