django jquery $.get to $.post

2019-08-25 08:52发布

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 ...>

1条回答
等我变得足够好
2楼-- · 2019-08-25 09:26

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

查看更多
登录 后发表回答