Integrate Django Admin¶
Problem¶
You want the Django admin and next.dj pages to coexist in the same project. The admin uses Django URL patterns and templates while the pages live under the file router.
Solution¶
Mount the admin under a path prefix above the file router in config/urls.py.
The Django URL resolver tries the admin patterns first.
Anything that does not match falls through to next.dj.
Walkthrough¶
Register the admin in urls.py.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("next.urls")),
]
Anything starting with /admin/ reaches the admin.
Every other URL reaches the file router.
Confirm INSTALLED_APPS¶
Add the admin and the auth apps that the admin depends on.
The caption below assumes a single settings module.
A project that split its settings places this in config/settings/base.py instead.
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"next",
"notes",
]
Register Models¶
Standard Django admin registration applies.
from django.contrib import admin
from notes.models import Note
@admin.register(Note)
class NoteAdmin(admin.ModelAdmin):
list_display = ("title", "created_at")
Link to Admin From the Site¶
Use reverse from inside the file router.
from django.urls import reverse
from next.pages import context
@context("admin_url")
def admin_url() -> str:
return reverse("admin:notes_note_changelist")
Use Frozen Form Specs Inside Admin¶
When the admin renders a custom form, next.forms.form_spec produces a frozen descriptor that admin templates can render without touching the standard Django widgets.
The repository ships a full admin integration. See the admin row in Repository Examples.
Verification¶
Run migrations and create a superuser.
uv run python manage.py migrate
uv run python manage.py createsuperuser
Start the server and visit /admin/.
The admin renders, the existing next.dj routes continue to work, and there is no overlap between the two URL spaces.
See Also¶
See also
Frozen Form Specs for frozen form specs.
Repository Examples for the admin sample project.