H_MZ 2018-02-27 10:46 采纳率: 0%
浏览 41

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)
  • 写回答

1条回答 默认 最新

  • weixin_33713707 2018-02-27 11:40
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码