CSRF verification failed. Request aborted. on django

。_饼干妹妹 提交于 2019-11-27 14:16:52
fcurella

You need to add the {% csrf_token %} template tag as a child of the form element in your Django template.

This way, the template will render a hidden element with the value set to the CSRF token. When the Django server receives the form request, Django will verify that the token matches the value that was rendered in the form. This is necessary to ensure that POST requests (i.e. data-altering requests) originate from an authentic client session.

For more info, check the Django documentation at: https://docs.djangoproject.com/en/dev/ref/csrf/

Here is an overview of the Cross-Site Request Forgery attack: https://www.owasp.org/index.php/CSRF

If you use csrf_token template tag but not change anything, check CSRF_COOKIE_DOMAIN setting. You should set None to it on development environment.

I had the same problem. I solved this problem when i added the {% csrf_token %}. Finally my code is this:

 <form id='formulario2' method='post' action='>
      <h3>Enter:</h3>
      {% csrf_token %}


     <input id="id_mesaje" name="mesaje" type="email" placeholder="E-mail"/>
    <input type='submit' name="boton2" value='Suscribete' style="display:inline-block;background-color: #80e174; "/>
 </form>

Just wanted give additional info on the topic. If it ever happens to you and you are sure that the token is injected in the form and the view functions are handling everything properly but the problem persists. Make sure that there is no javascript code disabling the input fields. Happened to me, after couple of hours of debugging, finally realized that.

<input type="hidden" name="csrfmiddlewaretoken" value="pHK2CZzBB323BM2Nq7DE2sxnQoBG1jPl" disabled="">
Patcho
{% csrf_token %}

inside your form. This worked out for me. So why do we use the Cross-site requested forgery?

Well, the answer is pretty simple, it just added another security layer to your web page, whereby any malicious user cannot validate a request using a wrong token.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!