简体   繁体   English

CSRF验证错误失败。 请求中止

[英]Error of CSRF verification failed. Request aborted

I have a code,there is an error of CSRF verfication failed, Request aborted. 我有一个代码,CSRF验证失败,请求中止。 In form: 通知:

<form method="POST" action="/jobb/" class="form-horizontal" id="jobform" name="jform" enctype="multipart/form-data" >{% csrf_token %}

In views.py 在views.py中

@csrf_exempt
def jobform(request):
    if request.method == 'POST':
        getintable = job(app_id = request.POST['jobid'],start_on = request.POST['starton'], end_on = request.POST['endon'],timeframe = request.POST['timeframe'],odeskid = request.POST['odeskid'],hourlyrate = request.POST['hourlyrate'],assigne = request.POST['assigne'],clientid = request.POST['clientid'])
        getintable.save()
        return render_to_response('jobsform.html')
    else:
        return render_to_response('interviewform.html')

You get this error because you are not returning a RequestContext instance. 因为未返回RequestContext实例,所以收到此错误。 To fix this you can use the render shortcut as Suhail suggested; 要解决此问题,您可以按照Suhail的建议使用render快捷方式 or you can pass in request context as a third argument to render_to_response . 或者,您可以将请求上下文作为第三个参数传递给render_to_response

In addition, you really should be using ModelForm , which automate a lot of the boilerplate code one would write. 另外,您确实应该使用ModelForm ,它可以自动执行很多人会编写的样板代码。

Here is how your code would look like: 您的代码如下所示:

You would put this code in a file called forms.py , which is in the same directory as views.py : 您会将这段代码放在一个名为forms.py的文件中,该文件与views.py处于同一目录中:

from myapp.models import Job

class JobForm(forms.ModelForm):
    class Meta:
        model = Job

In your views.py , you can do the following: 在您的views.py ,您可以执行以下操作:

from django.shortcuts import render, redirect

from myapp.forms import JobForm

def jobform(request):
    ctx = {'form': JobForm(request.POST or {})}
    if request.method == 'POST':
        if form.is_valid():
           form.save()
           return redirect('/some/url')
        else:
           return render(request, 'interviewform.html', ctx)
    else:
        return render(request, 'interviewform.html', ctx)

In interviewform.html : interviewform.html

<form method="POST"
      class="form-horizontal"
      id="jobform" name="jform" enctype="multipart/form-data">
      {% csrf_token %}
      {{ form }}
      <button type="submit" class="btn btn-primary"></button>
</form>

try using render, you don't even need csrf_exempt decorator. 尝试使用渲染,您甚至不需要csrf_exempt装饰器。 sinc you added csrf token in template( {% csrf_token %} ): sinc您在模板( {% csrf_token %} )中添加了csrf令牌:

from django.shortcuts import render

def jobform(request):
    if request.method == 'POST':
        getintable = job(app_id = request.POST['jobid'],start_on = request.POST['starton'], end_on = request.POST['endon'],timeframe = request.POST['timeframe'],odeskid = request.POST['odeskid'],hourlyrate = request.POST['hourlyrate'],assigne = request.POST['assigne'],clientid = request.POST['clientid'])
        getintable.save()
        return render(request,'jobsform.html')        
    return render(request,'interviewform.html')

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

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