Hello everyone - first post. I tried searching about this issue and didn’t find much; was wondering if I’m missing some configurations or if this is perhaps a bug? Though it feels like someone would’ve stumbled upon such a simple thing before but I couldn’t find much.
The TL:DR is that having a custom AdminSite in some app and then included in that app’s urls, if that app also defines an “app_name” in said urls, Django will throw a NoReverseMatch error when trying to access that admin page.
Now for a detailed explanation, assume the following Django project:
app/
├── app/
│ ├── settings.py
│ ├── urls.py
│ └── ...
└── my_custom_app/
├── admin.py
├── urls.py
└── ...
Where the files are, as expected:
# my_custom_app/admin.py
from django.contrib import admin
class AdminSite(admin.AdminSite):
pass
# my_custom_app/urls.py
from django.urls import path
from core.admin import admin_site
app_name = "my_custom_app" # this is the problematic line
urlpatterns = [
path("admin/", admin_site.urls, name="admin"),
]
# app/urls.py
from django.conf import settings
from django.urls import include, path
urlpatterns = [
path("", include("my_custom_app.urls")),
]
Note: my_custom_app
is installed in the settings as the last app
When trying to access <server_address>/admin
, instead of having access to the Django admin, the server will throw a “NoReverseMatch at /admin/” error, citing “‘admin’ is not a registered namespace”. Now, if you remove the “problematic line” (app_name = "my_custom_app"
from my_custom_app/urls.py
), the problem vanishes and everything works as expected.
Aditional details:
Django version: 4.1.5
I do have a bunch of other common modules installed, but they shouldn’t have anything to do with the issue I think:
rest_framework
rest_framework_simplejwt
rest_framework_simplejwt.token_blacklist
drf_standardized_errors
corsheaders
Am I doing something wrong?