保留原页面的参数条件

保留原页面搜索条件
  实现方式一:
    列表页面:
      获取当前所有条件,添加到
        - 编辑按钮的URL后面
        - 添加按钮的URL后面
    编辑或添加页面:
      - POST提交时,获取原来列表页面传过来的条件
      - 拼接URL /hosts/?原来的条件

    list页面:

      list_condition = request.GET.urlencode()

      {% for item in host_list %}
      <li>{{ item }} <a href="/edit/54/?{{ list_condition }}">编辑</a></li>
      {% endfor %}

    add/edit页面:http://127.0.0.1:8000/edit/10/?page=5&id__gt=4
      def edit_host(request,pk):
        if request.method == "GET":
          return render(request,'edit_host.html')
        else:
          # 修改成功 /hosts/?page=5&id__gt=4
          url = "/hosts/?%s" %(request.GET.urlencode())
          return redirect(url)

  实现方式二:
    list页面: http://127.0.0.1:8000/hosts/?page=5&id__gt=4

      params = QueryDict(mutable=True)  #如果是对象调用该方法(需要加下划线):params._mutable = True
      params['_list_filter'] = request.GET.urlencode()
      list_condition = params.urlencode()


      {% for item in host_list %}
      <li>{{ item }} <a href="/edit/54/?{{ list_condition }}">编辑</a></li>
      {% endfor %}

    add/edit页面:http://127.0.0.1:8000/edit/54/?_list_filter=page%3D5%26id__gt%3D4

      def edit_host(request,pk):
        if request.method == "GET":
          return render(request,'edit_host.html')
        else:
          # 修改成功 /hosts/?page=5&id__gt=4
          url = "/hosts/?%s" %(request.GET.get('_list_filter'))
          return redirect(url)

posted @ 2017-12-18 23:29  百连  阅读(267)  评论(0编辑  收藏  举报