4
respostas

csrf_token não é reconhecido

{% extends "base_usuario.html" %}
{% block body%}
    <form class="form-signin" action="{% url 'registrar' %}" method="post">

        <h2 class="form-signin-heading">Crie seu usuário</h2>
        <input id="id_email" type="text" name="email" class="form-control" placeholder="Email *" required>
        <input id="id_nome" type="text" name="nome" class="form-control" placeholder="Nome *" required>
        <input id="id_senha" type="password" name="senha" class="form-control" placeholder="Senha *" required>
        <input id="id_telefone" type="text" name="telefone" class="form-control" placeholder="Telefone">
        <input id="id_nome_empresa" type="text" name="nome_empresa" class="form-control" placeholder="Empresa">
        <hr/>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Registrar</button>
    </form>
{% endblock%}

o código está correto, mas o erro permanece como se não estivesse usando o {% csrf_token %}

...

Forbidden (403) CSRF verification failed. Request aborted. You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties. If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for 'same-origin' requests. Help Reason given for failure: CSRF cookie not set.

In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure: Your browser is accepting cookies. The view function uses RequestContext for the template, instead of Context. In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL. If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data. You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed. You can customize this page using the CSRF_FAILURE_VIEW setting.

4 respostas

Bom dia? Você esta usando a mesma versão de Django que uso no curso?

Bom dia? Você esta usando a mesma versão de Django que uso no curso? Versões mais novas, se não me engano, fazem diferente essa parte.

Porém, não estou vendo no seu formulário o {% csrf_token %}. Você adicionou no base?

<form action="{% url 'function_views'  %}" method="post">
{% csrf_token %}
</form>

Conseguiu? Você está usando a mesma versão do Django que uso no curso?

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software