简体   繁体   English

如何在按钮单击时对 Django ListView 进行排序

[英]How to Sort a Django ListView on Button Click

I'm making a website which has a bunch of Event posts (Parties, Get-Togethers, Fundraisers etc.).我正在制作一个包含大量活动帖子(派对、聚会、筹款活动等)的网站。

I want the user to be able to sort the posts by the date those event posts were created (created_date) and by the date that event post takes place (start_date) on button click.我希望用户能够按这些事件帖子的创建日期(created_date)和事件帖子发生的日期(start_date)在按钮单击时对帖子进行排序。

There are exactly 2 ways the user can sort the ListView (by created_date or by start_date according to my models.py) so I want the button to be a toggle, where clicking it once would filter by start_date and clicking the button once more (after the page refreshes) would filter by created_date.用户可以通过两种方式对 ListView 进行排序(根据我的 models.py 通过 created_date 或 start_date) ,所以我希望该按钮是一个切换按钮,单击它一次将按 start_date 过滤并再次单击该按钮(之后页面刷新)将按 created_date 过滤。

My home.html contains the button and a for loop to show the posts:我的 home.html 包含按钮和用于显示帖子的 for 循环:

<!-- THE EVENT FEED -->
<div class="feed">
  <h2>Event Feed</h2>
  <!--BUTTON TO FILTER EVENT FEED-->
  <div style = "border-top: solid 1px #eee; width:100%;align-items: center;display: flex;flex-direction: column;">

   <a href="{% url 'whsapp-home' %}?ordering={% if ordering == 'created_date' %}-start_date{% else %}-created_date{% endif %}"><button>Filter by Event Date</button></a>
   <!--<button data-text-swap="Filter by Event Date"></button>-->
  </div>

  <div class="chat">
    {% for post in posts %}
    <div class="your messages">
      <div class="message">
        <b>{{ post.title}}</b>
      </div>
    </div>
    {% endfor %}
    </div>
  </div>

And here is my class based view for the posts:这是我基于 class 的帖子视图:

class PostListView(ListView):
    model = Post
    template_name = 'whsapp/home.html' # FOLLOW THIS NAMING SCHEME <app>/<model>_<viewtype>.html
    context_object_name = 'posts'
    def get_ordering(self):
        ordering = self.request.GET.get('ordering','created_date') #Order live feed events according to closest start date events at the top
        return ordering

Does anyone know how I could go about implementing this?有谁知道我如何实现这个?

So I fiddled with the if statement some more and by making a 3 way conditional I was able to create the toggleable button所以我又摆弄了 if 语句,并通过设置 3 路条件,我能够创建可切换按钮

home.html:主页.html:

<a href="{% if request.get_full_path == '/' %}?ordering=-start_date{% elif request.get_full_path == '/?ordering=-start_date' %}/?ordering=-created_date{% elif request.get_full_path == '/?ordering=-created_date' %}/?ordering=-start_date{% endif %}"><button>Event Date</button></a>

views.py视图.py

class PostListView(ListView):
model = Post
template_name = 'whsapp/home.html' # FOLLOW THIS NAMING SCHEME <app>/<model>_<viewtype>.html
context_object_name = 'posts'
ordering = ['-created_date']
def get_ordering(self):
  ordering = self.request.GET.get('ordering','-created_date') #Order live feed events according to closest start date events at the top
  return ordering

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM