而在Django 1.8CSRF驗證失敗。請求中止,Django的POST請求
Forbidden (403)
CSRF verification failed. Request aborted.
我的網址更新數據庫中的記錄,我得到一個錯誤:
url(r'^blog/update/(?P<id>[0-9]+)/$','news.views.update')
def edit(request,id):
blogs = Blog.objects.get(pk=id)
return render_to_response('news/edit.html',{'blogs':blogs})
def update(request,id):
if request.method=='POST':
blog = Blog.objects.get(pk=id)
blog.title = request.POST.get('title')
blog.content = request.POST.get('content')
blog.save()
return HttpResponse('updated successfully!!')
else:
return HttpResponse('error')
新聞/ edit.html
<form action="/blog/update/{{blogs.id}}/" method="POST">{%csrf_token%}
<label>Title:</label>
<input type="text" name="title" value="{{blogs.title}}"><br>
<label>Content:</label>
<textarea cols="45" rows="4" name="content">{{blogs.content}}</textarea><br>
<input type="submit" value="submit">
</form>
做ü有 'django.middleware.csrf.CsrfViewMiddleware' 在你的中間件? – levi