QueraTeam / django-nextjs

Next.js integration for Django projects
MIT License
351 stars 18 forks source link

for next.js v12 thru Django channels, 'admin/' doesn't work #7

Closed crossz closed 2 years ago

crossz commented 2 years ago

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:

from django.contrib import admin
from django.urls import include, path, re_path
from django.views.generic.base import RedirectView
# from .views import jobs

# favicon_view = RedirectView.as_view(url='static/vercel.svg', permanent=True)

urlpatterns = [
    # path('', jobs),
    path('admin/', admin.site.urls),
    re_path(r'^vercel\.svg$', RedirectView.as_view(url='static/vercel.svg')),
    path('favicon.ico', RedirectView.as_view(url='static/favicon.ico')),  
]

all these 3 paths are not working if channels and django-nextjs enabled in INSTALLED_APPS in settings.py.

crossz commented 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.

danialkeimasi commented 2 years ago

Hi, can you show me your asgi.py file?

crossz commented 2 years ago

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)),
})
danialkeimasi commented 2 years ago

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)),
    }
)
crossz commented 2 years ago

super, all work fine now. I suggest publish this code into the README.md as settings for next.js v12 dev mode.

danialkeimasi commented 2 years ago

Yeah, I'm on it, thanks.