我有一个表格:<form action="{% url "some_url" %}" method="post"> {% csrf_token %} <input type="text">Text Input <button type="submit">Submit</button></form>这是通过 AJAX 提交的:$(function () { $('form').submit(function (e) { e.preventDefault(); $.post($(this).attr('action'), $(this).serialize(), function (response) { console.log(response); }); });});URL 路由到此视图:class SomeView(View): def post(self, request, *args, **kwargs): context = dict(**some_data) rendered_html = render_to_string('some_template.html', context, RequestContext(self.request)) return JsonResponse(dict(html=rendered_html))所有这些都有效。问题是它在未发送 CSRF 令牌时也有效,我得到了完全相同的成功响应:$.post($(this).attr('action'), function (response) { console.log(response);});我希望会出现某种错误,因为缺少 CSRF 令牌。陈述显而易见的:CsrfViewMiddleware在MIDDLEWARE_CLASSES.csrf_token当未发送令牌时,显式使用具有相同的结果:method_decorator(csrf_protect)def dispatch(self, request, *args, **kwargs): return super(SomeView, self).dispatch(request, *args, **kwargs)我可以做些什么来强制执行其验证?
添加回答
举报
0/150
提交
取消