Reputation: 188
I currently have this view which sends a list of genres to a template to populate a dropdown box:
class GenreSearch(generic.ListView):
template_name = 'games/genresearch.html'
context_object_name = 'genre_list'
def get_queryset(self):
return Genre.objects.order_by("name")
This is the template:
{% if genre_list %}
<div class="btn-group">
<button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Select Genre</button>
<div class="dropdown-menu scrollable-menu">
{% for genre in genre_list %}
<a class="dropdown-item" href="#">{{ genre.name }}</a>
{% endfor %}
</div>
</div>
{% endif %}
What I want to do now is select a genre from this dropdown box, submit it, and use this view to return results based on which genre is submitted:
class GamesPageByGenre(generic.ListView):
template_name = 'games/allgames.html'
context_object_name = 'game_list'
def get_queryset(self):
#Return games whose genres include the genre id x
return Game.objects.filter(genres__id=10)
So if the 'action' genre was selected from the dropdown box, submit this, get the ID for the genre action, then replace
genres__id=10
with genres__id=genreID
Upvotes: 0
Views: 47
Reputation: 98
Pass the url in dropdown:
<a class="dropdown-item" href="/gamespagebygenre/?genre={{ genre.id }}">{{ genre.name }}</a>
And in the view make like this:
def get_queryset(self):
genre_id = self.request.GET.get("genre")
return Game.objects.filter(genres__id=int(genre_id))
Upvotes: 1