Closed crossz closed 2 years ago
I do believe it's related to URLRouter(http_routes)
, within ProtocolTypeRouter
. This configuration will make urls.py along with settings.py failed to work.
Hi, can you show me your asgi.py
file?
This is the one I used and it's referenced from https://github.com/QueraTeam/django-nextjs/issues/4#issuecomment-1140307473 as I couldn't find useful usage for this part in the tutorial.
"""
ASGI config for mysite 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/4.0/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
# application = get_asgi_application()
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.http import AsgiHandler
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django_nextjs.proxy import NextJSProxyHttpConsumer, NextJSProxyWebsocketConsumer
from django.conf import settings
from django.urls import path, re_path
http_routes = []
websocket_routers = []
if settings.DEBUG:
http_routes.insert(0, re_path(r"^(?:_next|__next|next).*", NextJSProxyHttpConsumer.as_asgi()))
websocket_routers.insert(0, path("_next/webpack-hmr", NextJSProxyWebsocketConsumer.as_asgi()))
application = ProtocolTypeRouter({
"http": URLRouter(http_routes),
## Just HTTP for now. (We can add other protocols later.)
"websocket": AuthMiddlewareStack(URLRouter(websocket_routers)),
})
try this:
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
asgi_application = get_asgi_application()
from channels.auth import AuthMiddlewareStack
from channels.http import AsgiHandler
from channels.routing import ProtocolTypeRouter, URLRouter
from django.conf import settings
from django.urls import path, re_path
from django_nextjs.proxy import NextJSProxyHttpConsumer, NextJSProxyWebsocketConsumer
http_routes = [re_path(r"", asgi_application)]
websocket_routers = []
if settings.DEBUG:
http_routes.insert(0, re_path(r"^(?:_next|__next|next).*", NextJSProxyHttpConsumer.as_asgi()))
websocket_routers.insert(0, path("_next/webpack-hmr", NextJSProxyWebsocketConsumer.as_asgi()))
application = ProtocolTypeRouter(
{
"http": URLRouter(http_routes),
## Just HTTP for now. (We can add other protocols later.)
"websocket": AuthMiddlewareStack(URLRouter(websocket_routers)),
}
)
super, all work fine now. I suggest publish this code into the README.md as settings for next.js v12 dev mode.
Yeah, I'm on it, thanks.
after changing to channels with asgi.py, 'admin' listed in urls.py can not accessed correctly. any guidance about how to handle middleware like this? Thanks,
my urls.py is:
all these 3 paths are not working if channels and django-nextjs enabled in INSTALLED_APPS in settings.py.