AJAX Django获取请求

Can u help me, please! I need to render a new content, when i click on button without refresh page. Now I am using jQuery cookie plugin, save button id into cookie, then read in view that value and render page. But it does not look friendly :c

My 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();
});

My view:

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

In that case your will have to implement a REST endpoint which serves the frontend and using jQuery you have to modify the DOM to display the content.

Take a look at this tutorial and this tutorial.