Django server-side adapter for Inertia.js

Overview

django-inertia

GitHub Workflow Status (branch) Python Version PyPI License Code style: black

Django server-side new adapter for Inertia.js.

Getting Started

Install the package

pip install django-inertia

Configure your project

  1. Add the package django_inertia to your project (if you want to use the template tag else it's not necessary).

  2. Add InertiaMiddleware to your project middlewares:

MIDDLEWARES = [
  #...,
  "django_inertia.middleware.InertiaMiddleware",
]

Creating responses

To create and inertia response you need to use Inertia.render() method:

from django_inertia import Inertia

def event_detail(request, id):
    event = Event.objects.get(pk=id)
    props = {
        'event': {
            'id':event.id,
            'title': event.title,
            'start_date': event.start_date,
            'description': event.description
        }
    }
    return Inertia.render(request, "Event/Show", props)

Loading data into your template

{% inertia %} ">
{% load inertia_tags %}

<html  class="h-full bg-gray-200">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
    <script src="{{ STATIC_URL}}dist/app.js" defer>script>
    <link href="{{ STATIC_URL}}dist/app.css" rel="stylesheet" />
  head>
  <body>
    {% inertia %}
  body>
html>

Full documentation

TODO

Inertia.share()
Inertia.render()
Inertia.version()
Inertia.get_version()
Inertia.flush_shared()

Inertia.lazy()
Inertia.static()

Credits

Thanks to Andres Vargas for the inspiration on this package. Here is the link to its legacy package which seems not be actively maintained anymore: inertia-django

Contributing

TODO

Maintainers

License

django-inertia is open-sourced software licensed under the MIT license.

You might also like...
django-quill-editor makes Quill.js easy to use on Django Forms and admin sites
django-quill-editor makes Quill.js easy to use on Django Forms and admin sites

django-quill-editor django-quill-editor makes Quill.js easy to use on Django Forms and admin sites No configuration required for static files! The ent

A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, celery and redis.

Django Channels Websocket Chatbot A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, c

A handy tool for generating Django-based backend projects without coding. On the other hand, it is a code generator of the Django framework.
A handy tool for generating Django-based backend projects without coding. On the other hand, it is a code generator of the Django framework.

Django Sage Painless The django-sage-painless is a valuable package based on Django Web Framework & Django Rest Framework for high-level and rapid web

A beginner django project and also my first Django project which involves shortening of a longer URL into a short one using a unique id.

Django-URL-Shortener A beginner django project and also my first Django project which involves shortening of a longer URL into a short one using a uni

Dockerizing Django with Postgres, Gunicorn, Nginx and Certbot. A fully Django starter project.

Dockerizing Django with Postgres, Gunicorn, Nginx and Certbot πŸš€ Features A Django stater project with fully basic requirements for a production-ready

pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-django allows you to test your Django project/applications with the pytest testing tool.

APIs for a Chat app. Written with Django Rest framework and Django channels.
APIs for a Chat app. Written with Django Rest framework and Django channels.

ChatAPI APIs for a Chat app. Written with Django Rest framework and Django channels. The documentation for the http end points can be found here This

django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project. Inspired in the dashboard framework Dashing
django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project. Inspired in the dashboard framework Dashing

django-dashing django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project.

Django-MySQL extends Django's built-in MySQL and MariaDB support their specific features not available on other databases.
Django-MySQL extends Django's built-in MySQL and MariaDB support their specific features not available on other databases.

Django-MySQL The dolphin-pony - proof that cute + cute = double cute. Django-MySQL extends Django's built-in MySQL and MariaDB support their specific

Comments
  • Add type annotations

    Add type annotations

    This should be mostly correct, I'm a little unsure whether the request parameter in assertations.py is HttpRequest or something else though. I also fixed a potential issue for users that define their own setting for INERTIA_PAGE_CONTEXT. Closes #2

    Also just realized that my code should be formatted with black, I'll do that later. Let me know if you have any other style preferences too.

    opened by pmdevita 2
  • Generated value for data-page not JSON-parsable

    Generated value for data-page not JSON-parsable

    Got this error:

    Uncaught (in promise) SyntaxError: Unexpected token ' in JSON at position 1
        at JSON.parse (<anonymous>)
        at exports.createInertiaApp (index.js?4bf4:1:9213)
        at eval (app.js?5e94:7:17)
        at Module../demo/static/src/app.js (app.js:455:1)
        at __webpack_require__ (app.js:797:42)
        at app.js:861:37
        at app.js:863:12
    

    I believe this is because the generated data-page attribute string:

    <div id="app" data-page="{'component': 'Dashboard/Index', 'props': {'errors': False, 'success': False}, 'url': '/', 'version': '1'}"></div>
    

    is not valid JSON, hence the error.

    Valid JSON should be:

    • double quotes, not single quotes
    • False is not a valid keyword in JSON, but false is (with a lower cap "f")

    Below is a screenshot of the error: image

    No errors after manually editing the generated data-page attribute string to a valid JSON string:

    image

    bug 
    opened by mujahidfa 2
  • Use idiomatic Django to reference static files

    Use idiomatic Django to reference static files

    Hi @girardinsamuel!

    Just thought to improve the docs a bit by using Django's static template tag to reference the generated JS and CSS files in the template. I believe this is more idiomatic Django over using using {{ STATIC_URL }} directly in the template (unless there's another reason that I'm not aware of for it).

    Reference: https://docs.djangoproject.com/en/4.0/howto/static-files/#configuring-static-files

    Great work with this library btw πŸ‘πŸ‘

    Thanks!

    opened by mujahidfa 1
Releases(v1.3.0)
  • v1.3.0(May 12, 2022)

    Changed

    • Used idiomatic Django to reference static files by @mujahidfa in https://github.com/girardinsamuel/django-inertia/pull/6

    Fixed

    • Ensured valid JSON string is generated in the data-page attr by @mujahidfa in https://github.com/girardinsamuel/django-inertia/pull/8

    New Contributors

    • @mujahidfa made their first contribution in https://github.com/girardinsamuel/django-inertia/pull/6

    Full Changelog: https://github.com/girardinsamuel/django-inertia/compare/v1.2.0...v1.3.0

    Source code(tar.gz)
    Source code(zip)
  • v1.2.0(Apr 26, 2022)

    Changed

    • Add view_data keyword argument to Inertia.render() method allowing to pass context to django template and not to Inertia view by @muarachmann in https://github.com/girardinsamuel/django-inertia/pull/5
    • A new parameter INERTIA_PAGE_CONTEXT has been added which default to __page to define the context template variable used to pass data from backend to frontend.

    New Contributors

    • @muarachmann made their first contribution in https://github.com/girardinsamuel/django-inertia/pull/5

    Full Changelog: https://github.com/girardinsamuel/django-inertia/compare/v1.1.0...v1.2.0

    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Feb 17, 2022)

  • v1.0.1(Feb 11, 2022)

    Fixed

    • Fix default sharing of errors and success messages. They are now removed after being added to the response, so that they are really non persistent flash messages.

    Full Changelog: https://github.com/girardinsamuel/django-inertia/commits/v1.0.1

    Source code(tar.gz)
    Source code(zip)
Owner
Samuel Girardin
Full-Stack Engineer (Python, Vue.js)
Samuel Girardin
Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project

Django URL Shortener Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project Install this package to your Dja

Rishav Sinha 4 Nov 18, 2021
Django app for handling the server headers required for Cross-Origin Resource Sharing (CORS)

django-cors-headers A Django App that adds Cross-Origin Resource Sharing (CORS) headers to responses. This allows in-browser requests to your Django a

Adam Johnson 4.8k Jan 3, 2023
This is raw connection between redis server and django python app

Django_Redis This repository contains the code for this blogpost. Running the Application Clone the repository git clone https://github.com/xxl4tomxu9

Tom Xu 1 Sep 15, 2022
πŸ”₯ Campus-Run Django ServerπŸ”₯

?? Campus-Run Campus-Run is a 3D racing game set on a college campus. Designed this service to comfort university students who are unable to visit the

Youngkwon Kim 1 Feb 8, 2022
Meta package to combine turbo-django and stimulus-django

Hotwire + Django This repository aims to help you integrate Hotwire with Django ?? Inspiration might be taken from @hotwired/hotwire-rails. We are sti

Hotwire for Django 31 Aug 9, 2022
django-reversion is an extension to the Django web framework that provides version control for model instances.

django-reversion django-reversion is an extension to the Django web framework that provides version control for model instances. Requirements Python 3

Dave Hall 2.8k Jan 2, 2023
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.

Django-environ django-environ allows you to use Twelve-factor methodology to configure your Django application with environment variables. import envi

Daniele Faraglia 2.7k Jan 7, 2023
Rosetta is a Django application that eases the translation process of your Django projects

Rosetta Rosetta is a Django application that facilitates the translation process of your Django projects. Because it doesn't export any models, Rosett

Marco Bonetti 909 Dec 26, 2022
Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.

Cookiecutter Django Powered by Cookiecutter, Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly. Documentati

Daniel Feldroy 10k Dec 31, 2022
Django project starter on steroids: quickly create a Django app AND generate source code for data models + REST/GraphQL APIs (the generated code is auto-linted and has 100% test coverage).

Create Django App ?? We're a Django project starter on steroids! One-line command to create a Django app with all the dependencies auto-installed AND

imagine.ai 68 Oct 19, 2022