首页 > 代码库 > Django POST常见的错误:CSRF verification failed. Request aborted.
Django POST常见的错误:CSRF verification failed. Request aborted.
直接用代码说明吧。
在Post模板页面加入{%csrf_token%}
<html> <body> {{result}} <form action="{%url 'login'%}" method="post"> {% csrf_token %} name:<input type="text" id="username" name="username" value=http://www.mamicode.com/"super"/> >
把和POST页面相关的所有view的context强制换成RequestContextreturn render_to_response('wind/index.html',context,context_instance=RequestContext(request) )记得加入from django.template import RequestContext完整例子:from django.shortcuts import render from django.shortcuts import render,render_to_response from django.template import RequestContext from django.contrib.auth.models import User from django.contrib import auth from wind.models import UserProfile from django.core.context_processors import csrf # Create your views here. ''' index ''' def index(request): context={"js":"hello baby"} return render_to_response('wind/index.html',context,context_instance=RequestContext(request) ) ''' login ''' def login(request): username = request.POST['username'] password = request.POST['password'] user=auth.authenticate(username=username,password=password) if user is not None: if user.is_active: auth.login(request,user) context={'result':'login success!'} else: context={'result':'login failed!'} return render_to_response('wind/index.html',context,context_instance=RequestContext(request) )Django POST常见的错误:CSRF verification failed. Request aborted.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。