gpt4 book ai didi

javascript - AJAX django 获取请求

转载 作者:行者123 更新时间:2023-12-03 02:24:33 27 4
gpt4 key购买 nike

请你帮帮我吧!当我单击按钮而不刷新页面时,我需要呈现新内容。现在我正在使用 jQuery cookie 插件,将按钮 id 保存到 cookie 中,然后在 View 中读取该值并渲染页面。但看起来不太友好:c

我的JS:

function initBrandSelector() {
$('.tab button').click(function(){
var brand = $(this).val();
if (brand) {
$.cookie('current_brand', brand, {'path': '/', 'expires': 365});
} else {
$.removeCookie('current_brand', {'path': '/'});
}
location.reload(true);
return true;
});
}

$(document).ready(function(){
var brand = $.cookie('current_brand');
if (brand) {
$('.tab button[value=' + brand + ']').addClass("active");
}
initBrandSelector();
});

我的看法:

def smartphones_list(request):
current_brand = get_current_brand(request)
if current_brand:
smartphones = Smartphone.objects.filter(brand=current_brand)
else:
smartphones = Smartphone.objects.all()

context = paginate(smartphones, 6, request, {}, var_name='smartphones')
return render(request, 'main/smartphones_list.html', context)

最佳答案

@abybaddi009 I want to refresh content without reload page – Oleksii Petrushynskyi

在这种情况下,您必须实现一个为前端提供服务的 REST 端点,并使用 jQuery 修改 DOM 来显示内容。

看看这个 tutorial还有这个tutorial .

关于javascript - AJAX django 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49006795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com