Ajax Triggered request doesn't update Django View

后端 未结 1 631
天涯浪人
天涯浪人 2020-12-22 12:33

I tried to break this problem down into the simplest example. When the request is ajax, rendering the page with an updated context doesn\'t produce the expected result.

相关标签:
1条回答
  • 2020-12-22 13:12

    You can't return renders or redirects from AJAX, that's how it works

    If you want to update your UI based on something that happens on the server say you have a cart and you'd like to implement 'add to cart' without refreshing the page, the 'add to cart' button must request an endpoint you provided in your urls.py & that url must return true if object is added, or false if it wasn't, but it won't update it on the UI, you need to manually change the cart items count with Javascript.

    If you try to redirect or return a render to ajax, it will get the HTML or the redirect/render, nothing else.

    if you want to redirect, you'll want to do that with JS but with no context variables from django.

    see this ref

    0 讨论(0)
提交回复
热议问题