0
votes

Im working on a Django project and got stuck in a problem that involve csrf token.

I have a form that i handle the submit with javascript function, because in the same form i need to perform 2 POST. Form is something like that:

<form> 
{% csrf_token %}
   <input name="field_1" type="text">
   ....
   ....
   <input name="file" type="file">
   <button onclick="send_form()"> Send data </button>
</form>

the send_form() method performs two post request with axios. The first send the textfields and the second the file. I need to do this because the server two different api for manage the text data and the file.

The problem is that the first post succeeds and then the second fail, giving 403 error and in the header i can see the error

"CSRF Failed: CSRF token missing or incorrect."

There is a way to do this in a single form? I read that someone apply the csrf to the entire body of the page but i can't figure out how to do it.

Thank you for any answers.

1

1 Answers

0
votes

The reason why you are receiving that error is because you haven't specified what type of request you are making. This needs to be done in the <form> tag

for example : <form action="call_view_function" method="post">

If you are calling a views.py function, you should do so in the <form> tag and not onclick=