Skip to content

Instantly share code, notes, and snippets.

@adamchainz
Created January 8, 2021 14:34
Show Gist options
  • Save adamchainz/92e9706ddecb3e396b09048ca97f1dc8 to your computer and use it in GitHub Desktop.
Save adamchainz/92e9706ddecb3e396b09048ca97f1dc8 to your computer and use it in GitHub Desktop.
Django HTMX CSRF setup
<script src="{% static 'app.js' %}" data-csrftoken="{{ csrf_token }}"></script>
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