请你帮帮我吧!当我点击没有刷新页面的按钮时,我需要呈现一个新内容。现在我使用jQuery cookie插件,将按钮id保存到cookie中,然后在视图中读取值和渲染页面。但它看起来并不友好: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)