简体   繁体   中英

django jquery $.get to $.post

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM