,

Simplifying Django + Vite with a straightforward approach

Posted by



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!

0 0 votes
Article Rating
18 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Alex
1 year ago

I tested with vue and works like a charm! Thanks for sharing

harold quek
1 year ago

thanks for the video!

suyyrep
1 year ago

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' ?

Martino Massalini
1 year ago

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?

ALEX PIMENTEL MINGA
1 year ago

How i can integrate django with vuejs + vite.

SkyBlaze
1 year ago

too old template tag wont working anymore better provide full repo next time rather snippet

JJ
JJ
1 year ago

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?

Rodrigo Sepúlveda
1 year ago

great work!

Fabián Skármeta
1 year ago

i get sever error 500 on "production" 🙁

Gangadhari Nagarjuna
1 year ago

love to see (Django + Vite + Vuejs) integration

GD
GD
1 year ago

Could you please update video with django4.0 + vite+ vue3

Robert Cosmin Popa
1 year ago

for clarification, this is a scenario for SPA not for a normal django site?

Quentin Takeda
1 year ago

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/).

Wang Jack
1 year ago

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?

PaxNot
1 year ago

Fantastic video! Much appreciated!

Ravi Kunwar
1 year ago

Great content ,
Can you help me with images . . .
they are not working in django react vite app . .

Rachid Boudjema
1 year ago

Would really love to see (Django + Vite + Svelte) integration,

Thank you for the content

Tejesh P
1 year ago

Clear explanation. Appreciate it! Would love to see the svelte SPA integration with django & vite