ASGI 在 Django 中不启动。

huangapple go评论62阅读模式
英文:

ASGI does not start in Django

问题

I am following the tutorial by Dennis Ivy on YouTube: Django Channels & WebSockets Oversimplified

I want to make a simple website using websockets in Django. I am using Python 3.7.0

When I use the python manage.py runserver it outputs the following Starting development server at http://127.0.0.1:8000/
It should, however, output the following Starting ASGI/Channels version 3.0.0 development server at http://127.0.0.1:8000/

This is my settings.py file

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

ASGI_APPLICATION = 'myWebsite.asgi.application'

The asgi.py

import os

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

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

application = ProtocolTypeRouter({
    'http': get_asgi_application()
})
英文:

I am following the tutorial by Dennis Ivy on YouTube: Django Channels & WebSockets Oversimplified

I want to make a simple website using websockets in Django. I am using Python 3.7.0

When I use the python manage.py runserver it outputs the following Starting development server at http://127.0.0.1:8000/
It should, however, output the following Starting ASGI/Channels version 3.0.0 development server at http://127.0.0.1:8000/

This is my settings.py file

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


ASGI_APPLICATION = 'myWebsite.asgi.application'

The asgi.py

import os

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

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

application = ProtocolTypeRouter({
    'http':get_asgi_application()
})

答案1

得分: 1

ASGI 在 Django 中无法启动的问题通常在较新版本的 Channels 中经常发生。要解决此问题,您必须执行以下命令安装 Daphne:pip install daphne。安装完 Daphne 后,继续使用以下命令更新支持 Daphne 的 Channels:python -m pip install -U channels[“daphne”]

此外,非常重要的是将 'daphne' 添加到 Django 的 settings.py 文件中的 'INSTALLED_APPS' 列表顶部。这样可以确保 Django 在启动 ASGI 服务器时识别并优先使用 Daphne。

通过按照这些步骤操作,您应该能够成功解决问题,并且能够在 Django 中成功启动 ASGI。祝好运!

英文:

The problem you're facing with ASGI not starting in Django often occurs in newer versions of Channels. To resolve this issue, you must install Daphne by executing the following command: pip install daphne. Once Daphne is installed, proceed to update Channels with Daphne support using the following command: python -m pip install -U channels["daphne"].

Moreover, it is crucial to add 'daphne' at the top of the INSTALLED_APPS list in your Django's settings.py file. This ensures that Django recognizes and prioritizes Daphne when starting ASGI servers.

By following these steps, you should be able to resolve the issue and start ASGI successfully with Django. Good luck!

huangapple
  • 本文由 发表于 2023年5月22日 01:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定