Sunday, June 28, 2020

Re: CSRF verification failed, 'CSRF token missing or incorrect'

can i see the error log


On Mon, Jun 29, 2020 at 7:06 AM Shinster <sineadnicward@gmail.com> wrote:
When I try to submit a Django form with username and password, I get this error message:
'CSRF token missing or incorrect'.

The CSRF token is not missing at least.
Here is where I included it in my form in my .html template:

In **index.html:**

    <form name="loginform" action="/notes/index/" method="post">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit"/>
        {% for field, errors in form.errors.items %}
            {{ errors }}
       {% endfor %}
    </form>

In **views.py:**


    class LoginForm(forms.Form):
        username = forms.EmailField()
        password = forms.CharField(widget=forms.PasswordInput())

    def index(request):
         if request.method == 'POST':
         print("Received POST")
         form = LoginForm(request.POST)
         if form.is_valid():
            print("FORM is Valid")
         else:
            print("FORM is NOT VALID")
        template = loader.get_template('index.html')
        context = {
           'username': 'Benny',
           'form': LoginForm(),
        }
        return HttpResponse(template.render(context))

I tried adding @ensure_csrf_protect and @csrf_protect, but none of these worked. I also have csrf middleware in settings. I am trying to avoid using @csrf_exempt as this app will go into production down the line.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/11e4049b-cf04-4144-ad07-928c884ed812o%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFujGLye7TNFMs4673V2%3DpqdRd1%2BH3c27_1rbn1vDkQLxN4V5Q%40mail.gmail.com.

No comments:

Post a Comment