将JSON从模板发送到Django视图,然后重定向到该视图

发布于 2025-02-13 22:40:13 字数 1040 浏览 0 评论 0原文

我有一个模板 cat1.html ,其中列出了产品。 当客户选择项目时,它会构建具有所选产品属性的对象。 如果客户单击“添加到购物车”按钮,Axios将请求 + Params Stringifiend产品对象发送到 /CART

ulElement.addEventListener('click', function(event) {
  let targetClasses = Array.from(event.target.classList);
   
  if(targetClasses.includes('dropdown-item')) {
    /* code to build the item */
  }
  if(targetClasses.includes('btn-cart')) {
      axios.get('/cart', { params: { item: JSON.stringify(item) } } );
  }
});

到目前为止的工作正常,我可以从服务器中打印对象 /JSON,因此我知道它可以通过。

def cart(request):
    item_dict = json.loads(request.GET.get('item'))
    user_cart.append(item_dict)
    print(user_cart); # this seems to work fine
    
    /* return redirect('cart') this results in error TypeError(f'the JSON object must be str, bytes or bytearray, ' */

当我尝试将重定向添加到 /购物车视图时,问题就会发生。 我有错误

JSON对象必须是str,字节或bytearray,而不是非电视

我尝试使用window.location.href来解决此问题的非电视,并且还将按钮包裹在 中,但我得到了同样的错误,所以我感觉自己正在使用错误的方法。

I have a template cat1.html with a listing of products .
When a customer selects an item, it builds an object with properties of the selected product.
If the customer clicks 'add to cart' button, axios sends a request + params stringified product object to /cart

ulElement.addEventListener('click', function(event) {
  let targetClasses = Array.from(event.target.classList);
   
  if(targetClasses.includes('dropdown-item')) {
    /* code to build the item */
  }
  if(targetClasses.includes('btn-cart')) {
      axios.get('/cart', { params: { item: JSON.stringify(item) } } );
  }
});

so far this is working fine, I can print the object/json from the server so I know it makes it through.

def cart(request):
    item_dict = json.loads(request.GET.get('item'))
    user_cart.append(item_dict)
    print(user_cart); # this seems to work fine
    
    /* return redirect('cart') this results in error TypeError(f'the JSON object must be str, bytes or bytearray, ' */

The problem happens when I try to add a redirect to the /cart view.
I get the error

the JSON object must be str, bytes or bytearray, not NoneType

I've tried to get around this using window.location.href and also wrapping the button in an but I get the same error, so I get the sense I'm using the wrong approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撧情箌佬 2025-02-20 22:40:13

您无法将重定向发送为AJAX响应。您可以将消息或状态发送回消息,并且应该在成功回调功能的前面处理。

you can not send redirect as ajax response. You can send back message or status, and you should work with that on front in success callback function.

め可乐爱微笑 2025-02-20 22:40:13

这是因为您正在将参数中没有JSON的“购物车”视图重定向。因此,发生的事情是重定向是成功的,但是在重定向的实例中,“项目”参数是无,因此json.loads()引发了错误。

This is because you are redirecting to the 'cart' view without JSON in the parameters. So what happens is redirect is successful but then in the redirected instance, the 'item' parameter is None hence json.loads() throws the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文