python - Error of CSRF verification failed. Request aborted -
i have code,there error of csrf verfication failed, request aborted. in form:
<form method="post" action="/jobb/" class="form-horizontal" id="jobform" name="jform" enctype="multipart/form-data" >{% csrf_token %}
in 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 error because not returning requestcontext
instance. fix can use render
shortcut suhail suggested; or can pass in request context third argument render_to_response
.
in addition, should using modelform
, automate lot of boilerplate code 1 write.
here how code like:
you put code in file called forms.py
, in same directory views.py
:
from myapp.models import job class jobform(forms.modelform): class meta: model = job
in views.py
, can following:
from django.shortcuts import render, redirect 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>
Comments
Post a Comment