Created
January 8, 2021 14:34
-
-
Save adamchainz/92e9706ddecb3e396b09048ca97f1dc8 to your computer and use it in GitHub Desktop.
Django HTMX CSRF setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="{% static 'app.js' %}" data-csrftoken="{{ csrf_token }}"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const csrfToken = document.currentScript.dataset.csrftoken; | |
htmx.on('htmx:configRequest', (event) => { | |
if (csrfToken) { | |
evt.detail.headers['x-csrftoken'] = csrfToken; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment