禁止(403)CSRF验证失败。请求django 1.8.5中止2

时间:2016-02-03 06:59:36

标签: python django csrf

我知道这个问题很多时候都有问题。

我的CSRF令牌工作正常。但现在它给出了错误:

Forbidden (403) CSRF verification failed. Request aborted 2

我尝试了很多东西,例如删除cookie,历史记录以及在表单操作中更改完整的URL,但没有任何工作。

HTML 表单

 <form method="POST" action="/daily_sale/">
                        {% csrf_token %}
                        <div class="panel-heading">

                            <button class="btn btn-primary pull-right">Search</button>
                            <span class="pull-right">&nbsp;</span>
                            <span class="form-group pull-left">
                                <input type="text" class="form-control" name="start" placeholder="Start Date" value = "{{start_date}}">
                            </span>
                            <span class="pull-right">&nbsp;</span>
                            <span class="form-group pull-right">
                                <input type="text" class="form-control" name="end" placeholder="End  Date" value="{{end_date}}">
                            </span>
                        </div>
                    </form>

我还尝试更改form tag

中的操作
action="http://localhost:9002/daily_sale/"

view.py

from django.shortcuts import render , redirect


@login_required(login_url='/login_form/')
def dailySale(request):
user_id = request.user.id

reports = Reports()

if request.method == 'GET':
    # do some thing

if request.method == 'POST':
    print "inside post method"
    start_date = request.POST.get('start')
    end_date = request.POST.get('end')
    print "start  = ", start_date,"\n end  = ",end_date
    year = request.POST.get('year')
    month = request.POST.get('month')
    sale = reports.getSaleData(start_date,end_date,user_id)
    day = sale[0]
    sale_value = sale[1]
    sale_qty = sale[2]
    sale_data = zip(day, sale_value, sale_qty)

    sal = reports.get_sale_wise_channel(start_date,end_date,user_id)
    channel = sal[0]
    brand = sal[1]
    category = sal[2]
    selling_price = sal[3]
    quantity_sold = sal[4]
    percentage = sal[5]

    returns = reports.getReturns(start_date,end_date,user_id)
    order_item_ids = returns[1]
    order_date = returns[0]
    channel = returns[2]
    sku = returns[3]
    return_data = zip(order_item_ids, order_date, channel,sku)
    sal_data = zip(channel,brand,category,selling_price,quantity_sold,percentage)



    context_dict = {'sale_data':sale_data,
                     'sal_data':sal_data,                        
                     'start_date':start_date,
                     'end_date':end_date,
                    'month':month,
                    'year':year}
    return render(request, 'daily_sale.html', context_dict)

使用get方法工作正常,或者第一次运行时。

但是当我们尝试使用POST方法时,它会给出错误。

1 个答案:

答案 0 :(得分:1)

要解决(403) CSRF verification错误,您需要在每次呈现表单时更新请求上下文中的csrf toke:

所以将这些添加到你的GET和POST方法

context_dict.update(csrf(request))

并且不要忘记在您的观点顶部导入csrf:

from django.views.decorators.csrf import csrf_protect

如果您在处理表单时仍有问题,请考虑使用建议的ModelForms以及在django中处理表单的更强大方法。