简体   繁体   English

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

[英]Forbidden (403) CSRF verification failed. Request aborted 2 in django 1.8.5

I Know This question asked many time. 我知道这个问题问了很多次。

My CSRF token was working fine. 我的CSRF令牌运行正常。 but now it is giving error: 但现在它给出了错误:

Forbidden (403) CSRF verification failed. Request aborted 2

I try many things like removing cookie, history and changing the full url in form action but nothing is work. 我尝试了许多操作,例如删除cookie,历史记录和更改表单操作中的完整url,但没有任何效果。

HTML form 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>

I also try with changing action in form tag 我也尝试在form tag更改操作

action="http://localhost:9002/daily_sale/"

view.py 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)

Its working fine with get method or first time when it run. 它可以与get方法配合使用,也可以在首次运行时使用。

But when we try with POST method then it is giving error. 但是,当我们尝试使用POST方法时,它给出了错误。

To solve (403) CSRF verification error, you need to update your csrf toke in the request context each time that you render the form: 要解决(403) CSRF verification错误,您需要在每次呈现表单时在请求上下文中更新csrf令牌:

So add these to your both GET and POST methods 因此,将它们添加到您的GET和POST方法中

context_dict.update(csrf(request))

And don't forget to import csrf at top of your views: 并且不要忘了在视图顶部导入csrf:

from django.views.decorators.csrf import csrf_protect

If you still have problem in processing the form, consider using ModelForms which is recommended and more robust way of processing forms in django. 如果您在处理表单时仍然遇到问题,请考虑使用ModelForms ,这是推荐的,并且是在django中处理表单的更可靠的方法。

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

相关问题 禁止(403)CSRF验证失败。 请求在Django 1.11上中止 - Forbidden (403) CSRF verification failed. Request aborted on Django 1.11 禁止(403)CSRF验证失败。 请求中止。 Django的 - Forbidden (403) CSRF verification failed. Request aborted. Django 禁止(403)CSRF验证失败。 请求使用django中止 - Forbidden (403) CSRF verification failed. Request aborted using django 禁止(403)CSRF验证失败。 请求中止。在Django中 - Forbidden (403) CSRF verification failed. Request aborted.in Django 禁止(403)CSRF验证失败。 请求中止 - Forbidden (403) CSRF verification failed. Request aborted 禁止(403)CSRF验证失败。 请求中止 - Forbidden (403) CSRF verification failed. Request aborted Django:“禁止 (403) CSRF 验证失败。请求中止。” 在 Docker 生产中 - Django: "Forbidden (403) CSRF verification failed. Request aborted." in Docker Production 禁止Django注册(403)CSRF验证失败。 请求中止 - Django-registration Forbidden (403) CSRF verification failed. Request aborted 如何解决“禁止(403)CSRF验证失败。 请求中止。” Django中的错误 - How to fix “Forbidden (403) CSRF verification failed. Request aborted.” error in django 禁止 (403) CSRF 验证失败。 请求中止 - 与 Django 频道的实时聊天应用程序 - Forbidden (403) CSRF verification failed. Request aborted-Real time chat application with Django Channels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM