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.
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.
My home.html contains the button and a for loop to show the posts:
<!-- 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 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?