Django and Nuxt, match made in heaven
- Nuxt page is rendereder through Django
- user and permissions are injected to the generated Nuxt page
- Nuxt static files are served from Django
- In development mode, Nuxt and Django live reloads are working
- Django Debug Toolbar is working
- Nuxt DevTools are working (Django reverse-proxies DevTools on port 8000; Vite modules and fonts redirect to Nuxt)
- Nuxt SSR is not supported, yet
pip install django-nuxtIn the settings.py file, add the following:
TEMPLATES = [
...,
{
'BACKEND': 'django_nuxt.backends.NuxtDjangoTemplateBackend',
},
]
STATICFILES_FINDERS = [
...
'django_nuxt.staticfiles.NuxtStaticFilesFinder',
]and in the project's urls.py file, add the following:
from django_nuxt.urls import NuxtCatchAllUrls
urlpatterns = [
...,
] + NuxtCatchAllUrls()NuxtCatchAllUrls reverse-proxies unmatched HTML to the Nuxt development server when it is running so Django can inject window.django_nuxt. DevTools (/__nuxt_devtools__/) stay on the Django origin (usually port 8000). Vite modules (/_nuxt/) and fonts (/fonts/, /_fonts/) 302-redirect to Nuxt (usually port 3000) so HMR and assets skip Django's connection budget. If the Nuxt server is not running, Django loads the generated 200.html template.
For development (settings.DEBUG = True), run the Nuxt development server at the same time as Django. Open the app at http://localhost:8000/ — do not use port 3000 as the page origin.
The Nuxt module defaults the Django page origin to http://localhost:8000 (used for allowedHosts and CORS). Override it with nuxtDjango.devOrigin or the NUXT_DJANGO_DEV_ORIGIN environment variable.
For production, generate the Nuxt files with nuxt generate and then collect the static files with python manage.py collectstatic.
In production the Nuxt module points the generated JS and CSS at /static/_nuxt/ and the font files at /static/_fonts/. Set nuxtDjango.staticUrl to the same value as Django's STATIC_URL when that is not static/. Development still uses /_nuxt/ and /_fonts/.
The URL of the Nuxt development server that Django reverse-proxies to. Default is http://localhost:3000.
This value should be None or False in production.
The folder where the Nuxt generated files are stored. Default is ui/.output/public/.
The folder inside the DJANGO_NUXT_GENERATED_FOLDER folder that NuxtStaticUrls uses to serve the Nuxt _nuxt files. Default is _nuxt/.
NuxtStaticFilesFinder does not use this setting. collectstatic copies only the directory in DJANGO_NUXT_GENERATED_FOLDER named like the first segment of STATIC_URL (usually static), and it drops that name, so static/_nuxt/app.js is stored as _nuxt/app.js and static/_fonts/Inter.woff2 is stored as _fonts/Inter.woff2. Other generated directories and files such as 200.html are not collected. The Nuxt public folder is still collected as it is.
WhiteNoise serves STATIC_ROOT under STATIC_URL. Outside development, nuxt-django sets Nuxt's build assets directory to /static/_nuxt/ and the font files to /static/_fonts/, unless the app already set them. When Django's STATIC_URL is different, set nuxtDjango.staticUrl to that same value (assets/, /assets/, or a CDN URL such as https://cdn.example.com/static/). Generated files then live under that path in the Nuxt output. collectstatic drops the leading directory named like the first segment of STATIC_URL, and WhiteNoise serves the files from STATIC_URL. Development keeps /_nuxt/ and /_fonts/.
The folder where the Nuxt public files are stored. Will be collected to static files. Default is ui/public/.
A list of functions that will be called to process the data for the Nuxt page. Injected into window.django_nuxt object. Default is an empty list.
The prefix for the Django settings that will be injected to the Nuxt page. Default is NUXT_DJANGO_.
By default, injects the following settings:
- NUXT_DJANGO_BASE_URL
The name of the template that will be used to render the Nuxt page. This should not be changed. Default is _nuxt.
If you really need to fine tune the NuxtStaticUrls helper, you can set the DJANGO_NUXT_STATIC_URL to the prefix that will be used to serve the Nuxt static files. Default is an empty string.
Remember, you should not use NuxtStaticUrls in production!
Public origin the browser uses in development (the Django server). Default is http://localhost:8000. Set this on the Nuxt module when Django is not on port 8000:
export default defineNuxtConfig({
modules: ['@jrutila/nuxt-django'],
nuxtDjango: {
devOrigin: 'http://localhost:8000',
},
})Or export NUXT_DJANGO_DEV_ORIGIN=http://localhost:8000 when starting Nuxt.