Django + Vite: The simple way
Web development has come a long way in recent years, with technologies constantly evolving to make the process easier and faster. Django, a high-level Python web framework, has been a popular choice for many developers due to its robustness and scalability. Similarly, Vite, a build tool for modern web development, has been gaining popularity for its ability to provide a fast and efficient development experience. In this article, we’ll explore the simple way to combine Django and Vite for a seamless web development experience.
Django is known for its batteries-included approach, providing a lot of built-in features and components for rapid development. On the other hand, Vite focuses on fast development by leveraging the native ES module capabilities of modern web browsers. Combining these two technologies can result in a powerful and efficient development workflow.
To get started, we need to set up a Django project. Assuming you have Python and Django installed, open your terminal and create a new Django project using the following command:
“`
django-admin startproject myproject
“`
Next, navigate into the project folder:
“`
cd myproject
“`
Now, let’s create a new Django app within the project:
“`
python manage.py startapp myapp
“`
This will create a new app called “myapp” within the Django project.
With the Django app set up, we can now integrate Vite into the project. Start by installing Vite using npm:
“`
npm init @vitejs/app myapp –template vue
“`
This command will create a new folder named “myapp” with all the necessary files for a Vue.js-based Vite project.
Navigate into the newly created “myapp” folder and install the project dependencies:
“`
cd myapp
npm install
“`
Now, let’s configure Django to serve the Vite app. Open the `settings.py` file of your Django project and add the following lines:
“`python
STATICFILES_DIRS = [
os.path.join(BASE_DIR, ‘myapp’, ‘dist’)
]
“`
This tells Django to include the Vite app’s static files when serving the project.
Next, open the `urls.py` file within the Django app folder and add the following lines:
“`python
from django.views.generic import TemplateView
urlpatterns = [
# …
path(”, TemplateView.as_view(template_name=’index.html’)),
]
“`
This sets up a URL pattern that will render the `index.html` file of the Vite app when accessed.
Now, go back to the root folder of your Django project and run the development server:
“`
python manage.py runserver
“`
You should now be able to access your Django app with the integrated Vite project by navigating to `http://localhost:8000` in your web browser.
By combining Django and Vite, you can leverage the power of Django’s backend capabilities and Vite’s fast frontend development experience. You can easily integrate other front-end frameworks, such as React or Angular, by configuring Vite accordingly.
In conclusion, Django + Vite provides a simple and efficient way to develop web applications. With Django’s robustness and Vite’s fast development experience, you can create powerful and scalable web projects in no time. So give it a try and experience the benefits of this powerful combination!
I tested with vue and works like a charm! Thanks for sharing
thanks for the video!
has anyone tried this with vue? i got it running but how do i handle static files in development?
i tried STATIC_URL = '/static/' and STATICFILES_DIRS = [ VITE_APP_DIR / 'assets' ] but then i don't know what syntax i should use to load an image that's in my static folder
do i use import image from '@/assets/image.png' ?
Hi Luciano, thanks for the content, looking this as a django developer, can you point out the advantage of using vite as SPA inside django instead of using django as an api backend and vite as separate instances. Which part of django you can still leverage using this type of architecture?
How i can integrate django with vuejs + vite.
too old template tag wont working anymore better provide full repo next time rather snippet
Great video, I am working with a vue js template and django as backend, but not sure how to integrate them, is it the same as vite?
great work!
i get sever error 500 on "production" 🙁
love to see (Django + Vite + Vuejs) integration
Could you please update video with django4.0 + vite+ vue3
for clarification, this is a scenario for SPA not for a normal django site?
Hi Luciano, your video helped me A LOT ! Same as Ravi Kunwar, and you talk about it in the video, how can we handle the static files so ? Because you say "use the Django statics instead of vite statics", but if I do so, it works on developement, but the "npm run build" command won't work because vite isn't able to find my static images (because images are in core/static, not in src/).
Hello, nice job. But I am looking for a solution for SSR+Django. I want to render some data into HTML (like data= [data0, data1,….]), use react to read the JSON data in HTML and render it to a react component. Are there some suggestions?
Fantastic video! Much appreciated!
Great content ,
Can you help me with images . . .
they are not working in django react vite app . .
Would really love to see (Django + Vite + Svelte) integration,
Thank you for the content
Clear explanation. Appreciate it! Would love to see the svelte SPA integration with django & vite