-
-
Save mybigman/96a955b025bc6c3c1cfc570e3b85eea7 to your computer and use it in GitHub Desktop.
Django HTMX CSRF setup for htmx 1.1.0 and django 3.1
This file contains hidden or 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
{% 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/[email protected]"></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