We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 94516f2 commit 3e374b2Copy full SHA for 3e374b2
django_unicorn/static/js/unicorn.js
@@ -40,7 +40,17 @@ const Unicorn = (() => {
40
* Get the CSRF token used by Django.
41
*/
42
function getCsrfToken() {
43
+ // Default to looking for the CSRF in the cookie
44
+ const cookieKey = "csrftoken=";
45
+ const csrfTokenCookie = document.cookie.split(";").filter((item) => item.trim().startsWith(cookieKey));
46
+
47
+ if (csrfTokenCookie.length > 0) {
48
+ return csrfTokenCookie[0].replace(cookieKey, "");
49
+ }
50
51
+ // Fall back to check for the CSRF hidden input
52
const csrfElements = document.getElementsByName("csrfmiddlewaretoken");
53
54
if (csrfElements && csrfElements.length > 0) {
55
return csrfElements[0].getAttribute("value");
56
}
0 commit comments