0
votes

In my Django template I have a POST form. It includes the tag {% csrf_token %}. If I submit this form normally then everything works fine. However, I need to change a form input value via Javascript/Jquery:

$('input').val(id);

If I do this then I get the "CSRF verification failed" error message when I submit the form. I do submit the form via Javascript, but this has not been a problem so long as I don't dynamically change any values.

I have been "solving" this by writing @csrf_exempt above my Python functions. However, this skips the security. Is there another way?

1

1 Answers

0
votes

Command $('input').val(id); overwrites all input entries, which can be found, so hidden csrf_token input loses its value. Try to make more specific selector. For example:

$('input[name=change-me]').val(id);