Coder Social home page Coder Social logo

Comments (3)

adamchainz avatar adamchainz commented on July 4, 2024 1

There's no need for any Javascript. You can use hx-headers - this is already documented in the example project CSRF demo:

<section>
<p>
This form shows you how to implement CSRF with htmx, using <a href="https://dev.htmx.org/attributes/hx-headers/"> the<code>hx-headers</code> attribute</a>.
</p>
</section>
<section>
<!--
Note you can place the hx-headers attribute on your <body> tag so it
affects all requests, reducing repetition.
The header name may be changed with the CSRF_HEADER_NAME setting;
https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-CSRF_HEADER_NAME
Note we use {{ csrf_token }} instead of {% csrf_token %} to get the
variable rather than a rendered HTML <input>.
-->
<form hx-post="/csrf-demo/checker/"
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-target="#result">

I'm further adding a docs page in the Docs PR: https://github.com/adamchainz/django-htmx/pull/194/files#diff-3d3c9ddabd7ba7f0fa458351d17598c03548245af53febc5c09fd829bb42dea1

from django-htmx.

valberg avatar valberg commented on July 4, 2024

Oh yes I knew that :) But having to add that to each html tag where you want to do htmx requests "gets tedious". I started using hx-headers initially but kept on forgetting them, which is why I turned to the configRequest event - to remove friction :)

Anyway, I'll consider this as out-of-scope for django-htmx :)

from django-htmx.

valberg avatar valberg commented on July 4, 2024

Okay, my bad, I somehow totally missed the <body> part! That definitely makes my suggestion irrelevant! Thanks @adamchainz !

from django-htmx.

Related Issues (20)

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.