永发信息网

各位是怎么解决django的防csrf

答案:2  悬赏:30  手机版
解决时间 2021-03-08 21:48
  • 提问者网友:愿为果
  • 2021-03-08 16:47
各位是怎么解决django的防csrf
最佳答案
  • 五星知识达人网友:想偏头吻你
  • 2021-03-08 17:59
一种方法就是在模板的form表单添加:{% csrf_token %} 另一种方法:在MIDDLEWARE_CLASSES中注释掉'django.middleware.csrf.CsrfViewMiddleware'这个中间件。 如果解决了您的问题请采纳! 如果未解决请继续追问
全部回答
  • 1楼网友:酒者煙囻
  • 2021-03-08 18:22
django post出现403的解决办法 据说,从django1.x开始,加入了csrf保护。 csrf(cross-site request forgery跨站请求伪造,也被称成为“one click attack”或者session riding,通常缩写为csrf或者xsrf,是一种对网站的恶意利用。尽管听起来像跨站脚本(xss),但它与xss非常不同,并且攻击方式几乎相左。xss利用站点内的信任用户,而csrf则通过伪装来自受信任用户的请求来利用受信任的网站。与xss攻击相比,csrf攻击往往不大流行(因此对其进行防范的资源也相当稀少)和难以防范,所以被认为比xss更具危险性。-------来自百度百科 报错:forbidden (403) csrf verification failed. request aborted.help reason given for failure:csrf token missing or incorrect. in general, this can occur when there is a genuine cross site request forgery, or when django's csrf mechanism has not been used correctly. for post forms, you need to ensure: your browser is accepting cookies. the view function uses requestcontext for the template, instead of context. in the template, there is a {% csrf_token %} template tag inside each post form that targets an internal url. if you are not using csrfviewmiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the post data. you're seeing the help section of this page because you have debug = true in your django settings file. change that to false, and only the initial error message will be displayed. you can customize this page using the csrf_failure_view setting. 在网上找解决办法,说是提交参数中要有csrf_token,才能成功。但网上都是1.3或者1.4版本的解决办法,在1.5版本中测试已经不能用了。 在1.5.1版本,我测试可行的解决办法有三种: 一: 关闭csrf保护功能。为视图函数添加@csrf_exempt修饰符。 from django.views.decorators.csrf import csrf_exempt@csrf_exemptdef view(request): #your code..... 当然这样不安全。 二: 在模版文件中,每个form提交域中都加上{% csrf_token %}标签,并使用render函数返回视图,或者强行使用requestcontext 代替context。例: from django.shortcuts import renderdef contact(request): form = contactform()#这里我使用了一个django的表格 return render(request,'contact.html', {'form': form}) 或者: from django.shortcuts import render_to_responsedef contact(request): form = contactform()#这里我使用了一个django的表格 return render_to_response('contact.html', {'form': form}, context_instance=requestcontext(request) ) contact.html的内容: <html><head><style type="text/css"> ul.errorlist { margin: 0; padding: 0; } .errorlist li { background-color: red; color: white; display: block; font-size: 10px; margin: 0 0 3px; padding: 4px 5px; }</style> <title>send</title></head><body> <h1>contact us</h1> <form action="" method="post"> {% csrf_token %} <div class="field"> {{ form.subject.errors }} <label for="id_subject">工作:</label> {{ form.subject }} </div> <div class="field"> {{ form.email.errors }} <label for="id_email">你的邮箱地址:</label> {{ form.email }} </div> <div class="field"> {{ form.message.errors }} <label for="id_message">消息:</label> {{ form.message }} </div> <input type="submit" value="submit"> </form></body></html> 三: 方法二显然只能限制在django模版中使用,那如果我们使用javascript或者ajax的时候呢?怎么添加csrf_token呢? 我们可以使用javascript来提取cookies中的csrf_token。 function getcookie(name) { var cookievalue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jquery.trim(cookies[i]); if (cookie.substring(0, name.length + 1) == (name + '=')) { cookievalue = decodeuricomponent(cookie.substring(name.length + 1)); break; } } } return cookievalue; } 或者这个好理解的: function getcookie(sname){ var acookie=document.cookie.split("; "); for(var i=0;i<acookie.length;i++){ var acrumb=acookie[i].split("="); if(sname==acrumb[0])return (acrumb[1]); } return null;} ajax中这样用: $.post(url, {"csrfmiddlewaretoken":getcookie('csrftoken')}, function (data) {alert(data);}); 但是有一个问题,当有一个新用户访问这个页面的时候,cookie里并没有csrftoken这个值。只有进行第二种方法,才能在cookie里生成csrftoken值。解决此问题的方法随后更新。 完全可以满足简单的建站需要。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯