Coder Social home page Coder Social logo

django-htmx-refresh's Introduction

django-htmx-refresh

This is a Django app to handle full-page reloads with HTMX.

There are two possible use-cases for this app.

Quick start

  1. django-htmx-refresh relies on the django-htmx package. You can install it with pip:
    pip install django-htmx django-htmx-refresh
  1. Add "django_htmx_refresh" to your INSTALLED_APPS setting:
    INSTALLED_APPS = [
        ...
        'django_htmx_refresh',
    ]
  1. Include an app namespace for your project's urls:
    path('app_path', include(('app.urls', 'app_namespace'))),

Middleware

For when you want to handle htmx requests in all the views of a particular app.

  1. Create a new list in your settings called HTMX_APPS:
    HTMX_APPS = [
        'app_namespace'
    ]

This is so our custom middleware class only affects apps we explicity define, preventing errors with other third-party apps such as the default django admin application.

  1. Add HtmxResponseMiddleware to the MIDDLEWARE setting:
    MIDDLEWARE = [
        ...
        'django_htmx_refresh.middleware.HtmxResponseMiddleware',
    ]

Note: The HtmxReseponseMiddleware class provided with this application uses the process_template_response hook, meaning any view you intend to be used with this class must return a response object that implements a render method. Luckily Django's class-based views do this for us with TemplateResponse.

Mixin

For when you want to handle htmx requests in most of your views but not necessarily all of them.

  1. Import the HtmxResponseMixin and add it to your view(s).
    from django_htmx_refresh.mixins import HtmxResponseMixin

    class ExampleView(TemplateView, HtmxResponseMixin):
        template_name = 'template_name.html'

django-htmx-refresh's People

Contributors

johngwebdev avatar

Stargazers

Nikolaus Schlemm avatar Gavin Elie avatar Benjamin Kirkbride avatar Roger Pons avatar David Lesieur avatar  avatar

Watchers

Gavin Elie avatar  avatar

django-htmx-refresh's Issues

I am working on a awesome-python-htmx, seeking your feedback

A few of us at PyCon this year got together and brainstormed a new Web Stack that we are calling PyHAT (Python, htmx, ASGI, TailwindCSS). The first thing we set out to do is create awesome-python-htmx; a collection of active tools/libraries/projects in that space.

Your project seems is very interesting and exciting, but it seems to have it's development stalled? Also if I'm being honest I don't know that I fully understand what it does ๐Ÿ˜…. I'd appreciate your feedback if you have any on it's inclusion.

In addition to that, if you could also participate in PyHAT-stack/awesome-python-htmx#1 that would be greatly appreciated! Specifically any experiences you have had working with htmx and Python/Django.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.