DEV Community

Andrew Luchuk
Andrew Luchuk

Posted on

Simplify your frontend with Inertia JS

I sometimes want to integrate a frontend framework like Vue or React with Django.

There's only one major problem: maintainability. If I build the frontend of my application using a JavaScript framework, I will have to maintain multiple repositories and create APIs to facilitate communication between the frontend and the backend. Since I often develop projects on my own, setting up an API can be a prohibitive amount of extra work.

Fortunately there's a solution: Inertia JS. Inertia allows you to replace the view layer of your full stack framework with your frontend framework of choice.

What Inertia JS offers

Here are some other reasons you might want to try Inertia.

  • Simplicity: Inertia cuts out the complexity of an additional API layer between your backend and frontend. It also eliminates one of the more complex aspects of frontend frameworks in my experience: client-side routing.
  • A familiar full stack workflow: instead of communicating with the frontend using an API, Inertia uses the traditional approach: controllers and views.
  • Reduced development time: because you no longer need an API, you can reduce the amount of time needed to complete your application.
  • Simplified maintenance: the lack of an API layer means that maintaining your application is simpler due to the reduction in code dedicated to server-client communications.

You might think that by using the typical full stack approach, you would lose one of the core benefits of frontend frameworks: seamless transitions between pages. Surprisingly, Inertia is able to maintain seamless page-to-page transitions without sacrificing the traditional full stack structure.

Inertia is not a one-size fits all solution. If you intend to build an API for your backend for a reason other than purely communicating with the frontend, Inertia may be unnecessary. Similarly, if you do not need the seamless page-to-page transition user experience that frontend frameworks offer, Inertia probably isn't the best choice. If your site is primarily intended to present information to readers (like a CMS) and complex user interaction is not required, Inertia might introduce pointless complexity.

How to get started

That said, if Inertia would be useful for you, getting started with Inertia JS is pretty simple.

Inertia is designed for Laravel, so if you are building a Laravel application, then following the official setup documentation is the best way to get started.

On the other hand, setting up Inertia with Django is more complicated. The Inertia community has already built an adapter for Django, so you won't have to build your own tool kit to get Inertia and Django working together. Because Inertia is intended mainly for use with Laravel, the documentation for getting it set up with Django is
less descriptive and straightforward.

Fortunately, I have created a simple solution to get Inertia and Django up and running right away. I've created a GitHub repository template that you can use to scaffold Django with Inertia, React, and Tailwind CSS in just a few clicks. The first step is to go to the repository's GitHub page and click "Use this template". The template is configured so that Django should already have Inertia, React, and Tailwind CSS configured. If generating the project using a template would be helpful to you, please visit the repository to get started. I hope it helps you build your application in record time!

GitHub logo speratus / django-inertia-template-react

A template repository for bootstrapping a Django project with InertiaJS and React

Django Inertia Template (React)

This is a template repository for bootstrapping an application based on the following stack:

Usage

The first step for using this template is to create a new repository based on this template.

Once your new repository is created, you will likely want to take a number of customization steps:

  1. Change the name of the application. There are two parts to this a. Rename the webapp directory to the name of your application b. Update python references to the webapp package to the name of your application. For example, in the settings.py file, you will need to change the ROOT_URLCONF property and WSGI_APPLICATION property so that they reference the new package name instead of webapp.
  2. Rename the default app app to a more descriptive name. Similarly to step 1, you will also need to replace any python references to…

Top comments (0)