Django ListView自定义查询集 [英] Django ListView customising queryset

查看:168
本文介绍了Django ListView自定义查询集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这应该是一个可以帮助我的简单方法.

Hopefully this should be a simple one to help me with.

我有一个带有下拉菜单的页面,其中包含三个项目:

I have a page with a dropdown menu containing three items:

<form method="GET">

    <select name="browse">

        <option>Cats</option>

        <option>Dogs</option>

        <option>Worms</option>

    </select>

 <input type="submit" value="Submit" />

</form>

<!-- Output table -->

  <table id="myTable">

      <thead>
          <tr>
            <th>Name</th>
            <th>Colour</th>
          </tr>
      </thead>

      <tbody>
      {% for object in object_list %}
          <tr>
            <td>{{ object.name }}</td>
            <td>{{ object.colour }}</td>
          </tr>
      {% endfor %}
      </tbody>

  </table>

<!-- Pagination controls -->

<div class="pagination">
    <span class="page-links">
        {% if page_obj.has_previous %}
            <a href="?page={{ page_obj.previous_page_number }}">previous</a>
        {% endif %}
        <span class="page-current">
            Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
        </span>
        {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

当用户选择一个项目并点击提交时,就会在由通用ListView生成的表格中为他们提供结果:

When the user selects an item and hits submit, they are given the results in a table as generated by the generic ListView:

class Browse(generic.ListView):
    template_name = 'app/browse.html'
    paginate_by = 25

    def get_queryset(self):
        queryset = Cats.objects.all()
        if self.request.GET.get("browse"):
            selection = self.request.GET.get("browse")
            if selection == "Cats":
                queryset = Cats.objects.all()
            elif selection == "Dogs":
                queryset = Dogs.objects.all()
            elif selection == "Worms":
                queryset = Worms.objects.all()
            else:
                queryset = Cats.objects.all()
        return queryset

但是,当我尝试使用分页控件翻页时,由于(我认为)表单数据已重置,因此查询集会重置为第一个(默认)项Cats.

However, when I attempt to turn a page using the pagination controls, the queryset resets to the first (default) item Cats, because (I think) the form data is reset.

有什么想法可以解决这个问题吗?

Any idea how to circumvent this problem?

谢谢!

PS:哦,关于这一点,是否有可能首先将queryset设置为none?非常有义务!

PS: Oh, on that note, is it possible to set the queryset to none to begin with? Much obliged!

更新:当我在Cats查询集上使用分页时,它工作正常,因此该错误仅显示在其他两个集上.

UPDATE: When I use pagination on the Cats queryset it works fine so the bug is only displayed on the other two sets.

推荐答案

为解决此问题,我刚刚修改了分页HTML,以容纳来自表单的get请求和url字符串中的页码,如下所示:

To solve this problem I just modified the pagination HTML, to accommodate both the get request from the form and the page number in the url string, like so:

<div class="pagination">
    <span class="page-links">
        {% if page_obj.has_previous %}
            <a href="/browse/?browse={{ input }}&page={{ page_obj.previous_page_number }}">previous</a>
        {% endif %}
        <span class="page-current">
            Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
        </span>
        {% if page_obj.has_next %}
            <a href="/browse/?browse={{ input }}&page={{ page_obj.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

此处的{{input}}是一个字符串,其中包含通过表单提交的选项,例如猫"或蠕虫".

The {{ input }} here is a string containing the option submitted via the form, e.g. 'Cats' or 'Worms'.

为了能够将其传递到模板中,我修改了基于类的视图的get_context_data方法,如下所示:

To be able to pass this into the template, I modified the get_context_data method of the class based view as such:

class Browse(generic.ListView):
    template_name = 'app/browse.html'
    paginate_by = 25

    # Modifying the get_context_data method

    def get_context_data(self, **kwargs):
        context = super(Browse, self).get_context_data(**kwargs)
        q = self.request.GET.get("browse")
        context['input'] = q
        return context

    def get_queryset(self):
        queryset = Cats.objects.all()
        if self.request.GET.get("browse"):
            selection = self.request.GET.get("browse")
            if selection == "Cats":
                queryset = Cats.objects.all()
            elif selection == "Dogs":
                queryset = Dogs.objects.all()
            elif selection == "Worms":
                queryset = Worms.objects.all()
            else:
                queryset = Cats.objects.all()
        return queryset

就是这样,URL字符串现在显示为:

That was it, the url string now reads something like:

/browse/?browse=Cats&page=3

就这样,分页现在可以与表单的get方法一起使用.

So there it is, pagination now works alongside the get method of the form.

这篇关于Django ListView自定义查询集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆