0

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)
3
  • @Vaibhav id looks awful Commented Feb 27, 2018 at 11:19
  • What is the issue with the code? Commented Feb 27, 2018 at 11:30
  • @abybaddi009 I want to refresh content without reload page Commented Feb 27, 2018 at 11:32

1 Answer 1

1

@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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.