最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Django admin always redirected to static react app - Stack Overflow

programmeradmin2浏览0评论

I serve a static React app in Django like this:

# In /my_django_project/urls.py
urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r"^(?P<path>.*)$", my_django_app.views.serve_react, {"document_root": settings.REACT_APP_BUILD_PATH})]
# In /my_django_app/views.py
from django.views.static import serve as static_serve

def serve_react(request, path, document_root=None):
    path = posixpath.normpath(path).lstrip("/")
    fullpath = pathlib.Path(safe_join(document_root, path))
    if fullpath.is_file():
        return static_serve(request, path, document_root)
    else:
        return static_serve(request, "index.html", document_root)

According to Django's documentation, the URLs are matched in the order they are declared in urlpatterns and stop at the first match.

But if when I access http://127.0.0.1:8000/admin or http://127.0.0.1:8000/admin/, I always get redirected to my static React app homepage, despite the admin/ URL being first in the URL patterns (as if the ordering was not relevant).

If I comment out the static_serve line, the admin page get served as expected.

Why ? How to make the admin page served ?

I serve a static React app in Django like this:

# In /my_django_project/urls.py
urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r"^(?P<path>.*)$", my_django_app.views.serve_react, {"document_root": settings.REACT_APP_BUILD_PATH})]
# In /my_django_app/views.py
from django.views.static import serve as static_serve

def serve_react(request, path, document_root=None):
    path = posixpath.normpath(path).lstrip("/")
    fullpath = pathlib.Path(safe_join(document_root, path))
    if fullpath.is_file():
        return static_serve(request, path, document_root)
    else:
        return static_serve(request, "index.html", document_root)

According to Django's documentation, the URLs are matched in the order they are declared in urlpatterns and stop at the first match.

But if when I access http://127.0.0.1:8000/admin or http://127.0.0.1:8000/admin/, I always get redirected to my static React app homepage, despite the admin/ URL being first in the URL patterns (as if the ordering was not relevant).

If I comment out the static_serve line, the admin page get served as expected.

Why ? How to make the admin page served ?

Share Improve this question edited Mar 31 at 22:28 Phil 165k25 gold badges262 silver badges267 bronze badges asked Mar 31 at 22:23 PatopatoPatopato 335 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The re_path(r"^(?P<path>.*)$", ...) matches all URLs including /admin/ route.

Add ^(?!admin/) to exclude URLs starting with /admin/, ensuring the Django admin page loads correctly.

Modify urls.py like this:

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r"^(?!admin/)(?P<path>.*)$", my_django_app.views.serve_react, {"document_root": settings.REACT_APP_BUILD_PATH}),
]

PS: simply switching the order won't work because re_path(r"^(?P<path>.*)$", ...) is a catch-all pattern that matches everything

发布评论

评论列表(0)

  1. 暂无评论