我目前拥有此视图,该视图将流派列表发送到模板以填充下拉框:
class GenreSearch(generic.ListView):
template_name = 'games/genresearch.html'
context_object_name = 'genre_list'
def get_queryset(self):
return Genre.objects.order_by("name")
这是模板:
{% 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 %}
我现在要做的是从此下拉框中选择一种类型,提交,然后使用此视图根据提交的类型返回结果:
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)
因此,如果从下拉框中选择了“动作”类型,请提交此内容,获取该类型动作的ID,然后替换
genres__id=10
与genres__id=genreID
答案 0 :(得分:1)
在下拉列表中传递网址:
<a class="dropdown-item" href="/gamespagebygenre/?genre={{ genre.id }}">{{ genre.name }}</a>
在视图中,像这样:
def get_queryset(self):
genre_id = self.request.GET.get("genre")
return Game.objects.filter(genres__id=int(genre_id))