[Answer]-Django re-usable form csrf error

1👍

Try Adding the following to your code. I am not 100% sure, but I think it should work. Obviously, this disables the csrf protection. However, it should allow you to continue to run your website. While csrf is important, I am not sure that it is mandatory on every single view.

from django.views.decorators.csrf import csrf_exempt
    #Make this a function in your classview

        @csrf_exempt
        def dispatch(self, *args, **kwargs):
            return super(YOUR_VIEW_NAME_HERE, self).dispatch(*args, **kwargs)

0👍

For problem 1 (no field for username and password):

Are you sure the form object is in the context? It seems like there is no {{ form }} that the template has access to, so it can’t render {{ form.username }}

For Problem 2 (csrf):

You are including the {% csrf_token %} twice. Once in sample.html and once in login_snippet.html. Removing one of them might solve the issue.

Finally,

Also, is there any reason why the entire login form is not in one snippet? Why separate the ‘form’ element from the fields?

👤sid

Leave a comment