被csrf_token烦恼 - 虽然它在那里

时间:2014-03-09 05:20:28

标签: django django-templates django-views django-csrf

我不断错过csrf_token验证失败。我非常困惑/坚持这一点。正如你在下面的代码中所看到的,它实际上就在那里。任何帮助将非常感谢。 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>

1 个答案:

答案 0 :(得分:1)

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

找到答案

我在veiw中的post / get方法中缺少context_instance = RequestContext(request)

获得的经验:只是用户渲染。