Skip to content

Instantly share code, notes, and snippets.



Forked from adamchainz/_base.html
Last active Jan 8, 2021
What would you like to do?
Django HTMX CSRF setup for htmx 1.1.0 and django 3.1
{% load static %}
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{% block meta_tags %}
<meta name="csrf-token" content="{{ csrf_token }}">
{% endblock %}
{% block js %}
<script src=""></script>
const csrf_token = document.querySelector('meta[name="csrf-token"]').content;
htmx.on('htmx:configRequest', (evt) => {
if (csrfToken) {
evt.detail.headers['X-CSRFToken'] = csrf_token;
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment