Skip to content

Instantly share code, notes, and snippets.

@simkimsia

simkimsia/_base.html

Forked from adamchainz/_base.html
Last active Jan 8, 2021
Embed
What would you like to do?
Django HTMX CSRF setup for htmx 1.1.0 and django 3.1
{% load static %}
<!DOCTYPE html>
<html>
<head>
<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 %}
</head>
<body>
{% block js %}
<script src="https://unpkg.com/htmx.org@1.1.0"></script>
<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;
}
});
</script>
{% endblock %}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment