基于多个request.GET参数的过滤和排序

时间:2016-08-20 10:27:44

标签: python django sorting

我目前一直在努力寻找解决问题的方法。所以我有一个类似的URL:

https://www.domain.com/forum/topic/

在我的模板视图中,我有一个表单和一个输入,负责搜索帖子:

<form method="GET" action="">
    <div class="input-group">
        <input type="text" name="q" placeholder="Search..." value="{{ request.GET.q }}" class="form-control">
        <span class="input-group-btn">
            <input class="btn btn-secondary" type="submit" value="Search">
        </span>
    </div>
</form>

在我的 Views.py 中,搜索操作如下:

def discussion(request, discussion):
    topics_list = Topic.objects.all().filter(discussion__url=discussion)
    discussion = Discussion.objects.get(url=discussion)

    search_query = request.GET.get('q')

    if search_query:
        topics_list = topics_list.filter(
            Q(title__icontains=search_query) |
            Q(user__username__icontains=search_query)
            )

    paginator = Paginator(topics_list, 10)
    page = request.GET.get('page')
    try:
        topics = paginator.page(page)
    except PageNotAnInteger:
        topics = paginator.page(1)
    except EmptyPage:
        topics = paginator.page(paginator.num_pages)


    context = {'topics': topics, 'discussion': discussion,}
    return render(request, 'forum/forum_show_posts.html', context)

现在,当我运行搜索时工作正常,它实际上是根据我的查询过滤对象,从而使网址显示为:

https://www.domain.com/forum/topic/?q=test

现在我想为我的对象处理订单,所以我继续将讨论视图修改为:

def discussion(request, discussion):
    topics_list = Topic.objects.all().filter(discussion__url=discussion)
    discussion = Discussion.objects.get(url=discussion)

    search_query = request.GET.get('q')
    sort_query = request.GET.get('sort')

    if search_query:
        topics_list = topics_list.filter(
            Q(title__icontains=search_query) |
            Q(user__username__icontains=search_query)
            )

    elif sort_query:
        if sort_query == "newest":
            topics_list = topics_list.order_by('-timestamp')
        if sort_query == "oldest":
            topics_list = topics_list.order_by('timestamp')
        if sort_query == "name":
            topics_list = topics_list.order_by('title')

        # sort_query = sort_query.title()

    paginator = Paginator(topics_list, 10)
    page = request.GET.get('page')
    try:
        topics = paginator.page(page)
    except PageNotAnInteger:
        topics = paginator.page(1)
    except EmptyPage:
        topics = paginator.page(paginator.num_pages)


    context = {'topics': topics, 'discussion': discussion, 'sort_value':sort_query,}
    return render(request, 'forum/forum_show_posts.html', context)

和我的模板有每个订购方法的相应链接:

<div class="dropdown-menu">
    <a class="dropdown-item disabled" href="#">Sort...</a>
        <form method="GET" action="">
            <div class="input-group">
                <button class="dropdown-item" type="submit" name="sort" value="newest">Newest</button>
                <button class="dropdown-item" type="submit" name="sort" value="oldest">Oldest</button>
                <button class="dropdown-item" type="submit" name="sort" value="views">Views</button>
                <button class="dropdown-item" type="submit" name="sort" value="comments">Comments</button>
                <button class="dropdown-item" type="submit" name="sort" value="replies">Replies</button>
                <button class="dropdown-item" type="submit" name="sort" value="name">Name</button>
            </div>
        </form>
</div>

现在当我真正继续选择按最新或最旧订购时,它会对它们进行排序,使网址显示为:

https://www.domain.com/forum/topic/?sort=newest

我的问题是,我们想要搜索“测试”。制作网址

https://www.domain.com/forum/topic/?q=test

但是当我想要对搜索进行排序时,会被覆盖,而只是显示所有帖子,我选择对其进行排序。即使搜索已经存在,如何对其进行排序,如果没有搜索仍然对其进行排序。

https://www.domain.com/forum/topic/?q=testhttps://www.domain.com/forum/topic/?q=test&sort=newest所以它显示了最新的帖子列表,其中包含查询&#39; test&#39;。

1 个答案:

答案 0 :(得分:1)

您需要跟踪GET参数,将视图更新为:

def discussion(request, discussion): # <<- view name and var name both are same which might cause issues
    search_query = request.GET.get('q', '')
    sort = request.GET.get('sort', '')
    direction = request.GET.get('dir', 'asc')
    if direction not in ['asc', 'desc']:
        direction = 'asc'

    topics_list = Topic.objects.all().filter(discussion__url=discussion)
    discussion = Discussion.objects.get(url=discussion)

    if search_query:
        topics_list = topics_list.filter(
            Q(title__icontains=search_query) |
            Q(user__username__icontains=search_query)
        )

    if sort:
        order_by = '{0}{1}'.format('-' if direction == 'desc' else '', sort)
        topics_list = topics_list.order_by(order_by)

    # rest of code
    # pass search_query, sort and direction in context
    context = {
        'topics': topics,
        'discussion': discussion,
        'sort': sort,
        'direction': direction,
        'search_query': search_query,
    }

    return render(request, 'forum/forum_show_posts.html', context)

现在在模板中以两种形式跟踪这些参数:

搜索表单:

<form method="GET" action="">
    <div class="input-group">
        <input type="text" name="q" placeholder="Search..." value="{{ search_query }}" class="form-control">
        <span class="input-group-btn">
            <input class="btn btn-secondary" type="submit" value="Search">
        </span>
    </div>
    <input type="hidden" name="sort" value="{{ sort }}" />
    <input type="hidden" name="direction" value="{{ direction }}" />
</form>

排序表格:

<div class="dropdown-menu">
    <a class="dropdown-item disabled" href="#">Sort...</a>
        <form method="GET" action="">
            <div class="input-group">
                <button class="dropdown-item" type="submit" name="sort" value="newest">Newest</button>
                <button class="dropdown-item" type="submit" name="sort" value="oldest">Oldest</button>
               <button class="dropdown-item" type="submit" name="sort" value="views">Views</button>
               <button class="dropdown-item" type="submit" name="sort" value="comments">Comments</button>
               <button class="dropdown-item" type="submit" name="sort" value="replies">Replies</button>
               <button class="dropdown-item" type="submit" name="sort" value="name">Name</button>
            </div>

            <input type="hidden" name="search_query" value="{{ search_query }}" />
            <input type="hidden" name="direction" value="{{ direction }}" />

        </form>
</div>