如何 order/sort 过滤后的搜索查询并将其呈现在 Django 的另一个模板上?

How can I order/sort a filtered search query and render it on another template on Django?

我正在尝试更改 Django 上筛选搜索查询的顺序。我在搜索视图中使用基于 class 的 ListView。我能够从搜索中呈现过滤的查询集,但是如何更改具有相同搜索的相同查询集的顺序并将其呈现在另一个页面上。有点像 twitter 如何按热门或新的顺序搜索。我尝试制作不同的视图并更改顺序,但我不确定如何将相同的搜索查询转换为新视图。请帮忙!下面是我的代码。

views.py

class search_view(ListView):
    model = Post
    template_name = 'main/search.html'
    context_object_name = 'posts'
    paginate_by = 2
     # searches through everything using Q import 
    def get_queryset(self, *args, **kwargs):
          
          q = self.request.GET.get('q')
          self.posts = Post.objects.filter(
             Q(ticker__icontains=q) |
             Q(user__username__icontains=q) |
             Q(content__icontains=q) |
             Q(tags__name__icontains=q) 
          ).annotate(
               upvoted=Exists(Post.upvotes.through.objects.filter(
                    user_id=self.request.user.id,
                    post_id=OuterRef('pk')
            ))).order_by('-date_traded')
          return self.posts

template.html

<a class="btn btn-secondary dropdown-toggle mb-3 ml-2" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Sort
        </a>
        <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
            <a class="dropdown-item" href='?q={{ request.GET.q }}'>New</a>
            <a class="dropdown-item" href="#">Top</a> <!-- I would like to render the newly sorted results from here-->
        </div>

<!-- the get request for the query-->
<form class="form-inline" method="GET" action="{% url 'main:search' %}">
    <input class="form-control mt-2 mr-sm-2" type="search" placeholder="Search by ticker/tags" aria-label="Search" name="q">
    <button class="btn btn-outline-info mt-2 mr-sm-2" type="submit" value="Search">Search</button>
</form>

通常你会使用第二个 get 参数 order_by 让我们假设它看起来像: domain.com/view/?q=searchterm&order_by=-date_traded 将保持当前功能,如果你只是将 order_by 值传递给查询集排序。

此更改后,您可以在模板中添加任何带有 href 的标签,并将所需的 order_by 作为参数。

更新视图以支持第二个参数:

class search_view(ListView):
    model = Post
    template_name = 'main/search.html'
    context_object_name = 'posts'
    paginate_by = 2
     # searches through everything using Q import 
    def get_queryset(self, *args, **kwargs):
          q = self.request.GET.get('q')
          order_by = self.request.GET.get('order_by', '-date_traded')
          self.posts = Post.objects.filter(
             Q(ticker__icontains=q) |
             Q(user__username__icontains=q) |
             Q(content__icontains=q) |
             Q(tags__name__icontains=q)
          ).annotate(
               upvoted=Exists(Post.upvotes.through.objects.filter(
                    user_id=self.request.user.id,
                    post_id=OuterRef('pk')
            ))).order_by(order_by)
          return self.posts

和模板:

<a class="btn btn-secondary dropdown-toggle mb-3 ml-2" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Sort
        </a>
        <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
            <a class="dropdown-item" href='?q={{ request.GET.q
            }}&order_by=-date_traded'>New</a>
            <a class="dropdown-item" href='?q={{ request.GET.q
            }}&order_by=-other_field'>Top</a> <!-- I would like to render the newly sorted results from here-->
        </div>

<!-- the get request for the query-->
<form class="form-inline" method="GET" action="{% url 'main:search' %}">
    <input class="form-control mt-2 mr-sm-2" type="search" placeholder="Search by ticker/tags" aria-label="Search" name="q">
    <button class="btn btn-outline-info mt-2 mr-sm-2" type="submit" value="Search">Search</button>
</form>

编辑。所以澄清一下,我相信相同的视图和相同的模板应该适用于这个用例,只是查询集顺序不同。如果没有,请更新要求。

我也遇到过类似的情况。我尝试使用 FBV。下面的代码对我的案例进行了排序:

def songs(request, filter_by):
if not request.user.is_authenticated:
    return render(request, 'music/login.html')
else:
    try:
        song_ids = []
        for album in Album.objects.all():
            for song in album.song_set.all():
                song_ids.append(song.pk)
        users_songs = Song.objects.filter(pk__in=song_ids)
        if filter_by == 'favorites':
            users_songs = users_songs.filter(is_favorite=True)
    except Album.DoesNotExist:
        users_songs = []
    return render(request, 'music/songs.html', {
        'song_list': users_songs,
        'filter_by': filter_by,
    })

希望对您有所帮助。正如@david 所说,您不需要其他模板或视图。