user133688
user133688

Reputation: 7064

Vexed by csrf_token -- though it's there

I keep getting a verification failed missing csrf_token. I'm very puzzeled/stuck on this. as you can see by my code below it's in fact there. Any help will be very much appreciated. Django==1.6.1 btw

MIDDLEWARE_CLASSES = (.... 'django.middleware.csrf.CsrfViewMiddleware', ...)

class BaseSignUp(View):
    model = None
    form = None
    template = None

    def get(self, request):
        return render_to_response(self.template,
           {'user_form': UserCreateForm, 'form': self.form})

    def post(self, request):
       user = UserCreateForm(request.POST)
       obj = self.form(request.POST)
       if user.is_valid():
          user.save()
       else:
          return render_to_response(self.template, {'user_form': user, 'form': obj})


<form method="post">
    {% csrf_token %}
    <table>
    {{ user_form }}
    {{ form }}
    </table>
    <button type="submit">Save</button>
</form>

Upvotes: 1

Views: 49

Answers (1)

user133688
user133688

Reputation: 7064

Found the answer here Django - {% csrf_token %} was used in a template, but the context did not provide the value

I was missing the context_instance=RequestContext(request) in the post/get methods in the veiw

Lesson learned: just user render.

Upvotes: 1

Related Questions