[Answer]-Error of CSRF verification failed. Request aborted

1👍

You get this error because you are not returning a RequestContext instance. To fix this you can use the render shortcut as Suhail suggested; or you can pass in request context as a third argument to render_to_response.

In addition, you really should be using ModelForm, which automate a lot of the boilerplate code one would write.

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:

from myapp.models import Job

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

In your views.py, you can do the following:

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:

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

0👍

try using render, you don’t even need csrf_exempt decorator. sinc you added csrf token in template({% csrf_token %}):

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')

Leave a comment