django jquery $ .get到$ .post [英] django jquery $.get to $.post

查看:203
本文介绍了django jquery $ .get到$ .post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在django中我有

  #template 
$ .get(/ places / {{place.id }} / save /,{description:cadena}

#view
place.description = request.POST.getlist('description')[0]

work ok。
但如果尝试更改为 $。post

  #template 
$ .post(/ places / {{place.id}} / save /, {description:cadena}

#view
print request.POST



<我的问题,我没有添加 context_instance

=发送 $。post 这个 crsf_token 视图中的RequestContext(request)不存在。



这个变化现在可以使用

  $ .post(/ places / {{place.id}} / save /,{description:cadena,csrfmiddlewaretoken:'{{csrf_token}}'} 

并且是必要的 {{csrf_token}} 不是 {%csrf_token%}

{%csrf_token%} 创建< input ...> csrf_exempt 。关于此的更多信息可以在这里找到:
https:/ /docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax


in django I have

#template
$.get("/places/{{ place.id }}/save/",{description : cadena }

#view
place.description = request.POST.getlist('description')[0]

work ok. but If try change to $.post

#template
$.post("/places/{{ place.id }}/save/",{description : cadena }

#view
print request.POST

nothing happend

solved

my problem, I don't added context_instance=RequestContext(request) in the view of send the $.post for this crsf_token don't exist.

with this change now work

 $.post("/places/{{ place.id }}/save/",{description : cadena, csrfmiddlewaretoken: '{{ csrf_token }}'}

and is necessary {{ csrf_token }} not {% csrf_token %}.
{% csrf_token %} create a <input ...>

解决方案

You're probably running into a Cross Site Request Forgery (CSRF) problem - Django is rejecting the POST because there's no CSRF token. For Ajax, you will need to do some special handling, or mark the view as csrf_exempt. More information about this can be found here: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

这篇关于django jquery $ .get到$ .post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆