I'm surely not maintaining this as well as I could. There are also other possible, better integrated solutions, like django-vite, so keep in mind this was supposed to be more of a note to myself than anything :]
This gist has most of the things I've used to develop the frontend using vite inside a monolithic django app.
Here's a boilerplate that uses this approach: https://github.com/labcodes/django-react-boilerplate
A couple of things to note:
- it runs in SPA mode by default. If you want SSR, you may want to look into django_vite;
- static files unrelated to your app, like images or fonts, should be served by django, instead of imported directly inside your frontend app;
- you'll need to enable manifest in your vite configs for production;
I've only tested this with a React app, but I'll share my notes when testing with a Svelte app later.- I've already tested with svelte, and the changes are really small! For the most part, you'll only need to remove react-specific stuff (like react-refresh), use a svelte + vite app as base, and change
base.htmlto importmain.jsinstead ofjsx. And I've created a sample template for it too!
- I've already tested with svelte, and the changes are really small! For the most part, you'll only need to remove react-specific stuff (like react-refresh), use a svelte + vite app as base, and change
Besides that, just read the comments carefully and everything should work out. Whenever I'm free, I'll make a video about this and add the URL here :]
I've published a video on how to do it, step-by-step: https://www.youtube.com/watch?v=FCyYIVfDkhY
Hi, thanks a lot for this gist! I am using Django+vue3+vite, so I'd like to leave here the changes I made for it to work, for reference.
Context: I added
vueappdirectory in the root of my django project, with vite.config.js inside it. I use django to serve the index.html (Vue's entrypoint), so that I can use django to protect it with login required.I created a django view to serve the vueapp.html template:
{% load render_vite_bundle %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700" rel="stylesheet" /> <link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:400" rel="stylesheet" /> <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" /> <link rel="icon" href="/static/vueapp/favicon.ico" /> <title>Vite App</title> {% if debug %} <script type="module" src="http://localhost:3000/@vite/client"></script> {% endif %} </head> <body> <div id="app"></div> {% if debug %} <!-- This url will be different for each type of app. Point it to your main js file. --> <script type="module" src="http://localhost:3000/src/main.ts"></script> {% else %} {% render_vite_bundle %} {% endif %} </body> </html>I changed the render_vide_bundle.py a little to add all scripts and css's.
My django
settings.py