Asked 1 year ago by VenusianResearcher126
Why is Django reporting a missing CSRF token even though it's present?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 year ago by VenusianResearcher126
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I have a Vue app that makes requests to several Django Rest Framework endpoints. It was working fine yesterday when I could submit and save content, but today I keep getting the error: CSRF Failed: CSRF token missing.
When I inspect the network traffic in dev tools, I see the following cookies are set:
csrftoken: "wzWBo6N66F1HTp2To67We5GppNWfbXdm"
sessionid: "1vq4vj73b1b6l80gt2ckba6zwxqj8wwu"
How is the CSRF token considered missing when it is clearly there?
Additionally, I have configured these settings, but I still encounter the CSRF error. What role do these configurations play in this situation?
PYTHONCORS_ORIGIN_WHITELIST = ("localhost:8080", "127.0.0.1:8080") CSRF_TRUSTED_ORIGINS = ["http://localhost:8080", "http://*.127.0.0.1", "http://127.0.0.1:8080", "http://192.168.1.44:8080",]
Comments 0
•Answers 1
•Views 13.8k
Answer by TychoAI Agent • 9 days ago
The issue you're experiencing is common with Django's CSRF protection. Even though the csrftoken
cookie is set, Django is still reporting it as missing. This typically happens because the token isn't being properly included in your request headers.
When making non-GET requests to Django (POST, PUT, DELETE, etc.), you need to:
csrftoken
cookie (which you do)In your Vue app, you need to extract the token from cookies and include it in your requests:
JAVASCRIPT// Get the CSRF token from cookies function getCSRFToken() { const cookieValue = document.cookie .split('; ') .find(row => row.startsWith('csrftoken=')) ?.split('=')[1]; return cookieValue; } // Include it in your axios requests axios.defaults.headers.common['X-CSRFToken'] = getCSRFToken(); // Or for individual requests axios.post('/your-endpoint/', data, { headers: { 'X-CSRFToken': getCSRFToken() } });
Your configuration settings have these issues:
PYTHONCORS_ORIGIN_WHITELIST = ("localhost:8080", "127.0.0.1:8080") # Missing protocol
This should include the protocol:
PYTHONCORS_ORIGIN_WHITELIST = ["http://localhost:8080", "http://127.0.0.1:8080"]
Your CSRF_TRUSTED_ORIGINS
looks correct, but ensure you're using the correct protocol (http vs https).
Ensure the CSRF middleware is enabled in your Django settings:
PYTHONMIDDLEWARE = [ # ... 'django.middleware.csrf.CsrfViewMiddleware', # ... ]
Modern browsers enforce strict SameSite cookie policies. If your frontend and API are on different domains or ports, you might need to adjust:
PYTHONCSRF_COOKIE_SAMESITE = 'Lax' # Or 'None' if using HTTPS SESSION_COOKIE_SAMESITE = 'Lax' # Or 'None' if using HTTPS # If using 'None', you must also set secure=True if CSRF_COOKIE_SAMESITE == 'None': CSRF_COOKIE_SECURE = True SESSION_COOKIE_SECURE = True
Since it was working yesterday and stopped today, check if any browser updates occurred or if any settings were changed in your application.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.