django jquery $ .get到$ .post [英] django jquery $.get to $.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)问题 - Django拒绝了这个问题。 POST,因为没有CSRF令牌。对于Ajax,您需要做一些特殊处理,或者将视图标记为
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屋!