If I send an AJAX POST request from a button. Do I need to validate the CSRF token in my view or does Django automatically do this for you when it accepts the POST? If not, how do I manually validate it myself?
Also, is it correct that when you create a Form and pass in the request, the Form object will automatically do this validation?
jQuery:
<script>
$(document).ready(function() {
$('#button1').click(function(){
$.post("/", {
unique_id : "{{ unique_id }}",
csrfmiddlewaretoken: '{{ csrf_token }}'
});
});
});
</script>
If you have
'django.middleware.csrf.CsrfViewMiddleware',inMIDDLEWARE_CLASSESin your projectsettings.pythenCsrfViewMiddlewareis automatically validate thecsrf_token. This is mentioned in the documentation also: