Docs: Possible import error in a Tutorial Partr 2 example
jalaj711 opened this issue · comments
I am a starter with Django Channels and while navigating the docs, I noticed that there is an issue in one of the examples on part 2 of the Chat Room tutorial. In this example where you talk about adding routing to the asgi.py file, the actual routings are not imported.
Not importing the chat.routing.websocket_urlpatterns
first gives the impression that this should be inside double quotes and that the URLRouter
works similiar to the include()
function where you give module names as strings. However, I discovered that this is not the case and you have to import the module seperately.
To make things simpler for beginners, I beleive that the example should be changed to...
# mysite/asgi.py
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
from chat.routing import websocket_urlpatterns
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()
import chat.routing
application = ProtocolTypeRouter(
{
"http": django_asgi_app,
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
),
}
)
...as this seems to be functioning perfectly fine.
I would like to have the opinions from the contributors on this, and if it feels right, I would be more than happy to make a PR with this change.
Thanks for the report. Sounds about right 🙂
Would you like to make a PR?
Yes, sure!