Giter Site home page Giter Site logo

django-ex's Introduction

What is this?

This is intended as an unofficial Django extension package that adds support to a few HTMX functionalities. By default, Django ignores request parsing for all methods that are not GET and POST, this middleware extends that behaviour to PATCH and PUT HTMX ajax requests.

Since it simulates existing django infrastructure, you should be able to use and rely on the usual Django request behaviour the same way you do in vanilla, only with automatically parsed request bodies for those aditional methods.

Your vanilla Django views now work with aditional request methods!

Who is this for?

You already use HTMX and wants aditional request method flexibility.

Who is this not for?

Your project uses REST apis or require robust content-type support which would be better served by standalone packages such as Django Rest Framework.

How do i use it?

You should be used to acessing data in request.POST. The aditional methods follow in similar fashion, request.PATCH and request.PUT.

Data is also unifiably available in request.data, regardless of request method.

Tips

One of the caveats is with the security CRSF token. You need to explicitly request HTMX to pass the token as a request header.

In your templates you are likely used to doing something like this:

<form hx-post="/route">
    {% csrf token %}
    <-- rest of your form -->
</form>

For aditional HTTP methods to work you need to pass it as such:

<form hx-patch="/route" hx-headers="{{csrf token}}">
    <-- rest of your form -->
</form>

Since the HX-headers attribute is inherited by nested HTML elements it is also possible to define it in a base template:

<body hx-headers="{{csrf token}}">
    <form hx-patch="/route">
        <-- rest of your form -->
    </form>
    <-- rest of your page -->
</body>

Installation guide

# download the package
python3 -m pip install django-ex

# in your projects settings.py include at the end:
...
MIDDLEWARE = [
    ...
    "django_ex.middleware.http.ProtocolExtensionMiddleware",
]
...

django-ex's People

Contributors

vln37 avatar

Stargazers

Paco Rocha avatar Eder Sosa avatar Sheela G. Khusal avatar Henrique Rocha avatar Gabriel Vitor avatar Paulo Santana avatar Welton Leite avatar

Watchers

 avatar

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.