简体   繁体   English

CSRF验证失败。 请求中止。 Django 2.0

[英]CSRF verification failed. Request aborted. Django 2.0

def order_view(request):
    if request.method == 'POST':
        form = OrderForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('Order Submitted')
    else:
        form = OrderForm()

    return render_to_response('home/order.html', {'form': form})

order_view function in views.py views.py中的order_view函数

<form class="form form-table" method="post">
{% csrf_token %}
{{ form|crispy   }}
<input class="btn br-green" type="submit" value="Submit"/>
</form>

There is still a CSRF error in it. 仍然存在CSRF错误。 Have tried most of the solution but they are not working.Have tried adding RequestContext(request) as well. 尝试了大多数解决方案,但它们不起作用。还尝试添加RequestContext(request)。

HttpResponseRedirect takes a url. HttpResponseRedirect采用网址。 I don't think 'Order Submitted' is. 我认为“提交的订单”不是。

Try 尝试

def order_view(request):
    if request.method == 'POST':
        form = OrderForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = OrderForm()
    return render_to_response('home/order.html', {'form': form})

If this works then you're sorted and use django.messages to provide the message to your user. 如果可行,则对您进行排序并使用django.messages向您的用户提供消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM