0

In Django I have a form that allow the user to filter objects in a database, this works great when the user chooses one value for parameter but I would like to allow multiple choices.

I've tried using checkbox or select multiple, both don't work. I've tried writing a view function with multiple if statement for every possible combination of choices, it works but it's definetely a bad practice. I'm reading the Django tutorial but still haven't found a solution for what I think it's a simple problem.

#my models.py
class Game(models.Model):
  name = models.CharField(max_lenght=100)
  platform = models.CharField(max_lenght=100)
  is_free = models.BooleanField(default=True)
  genre = models.CharField(max_lenght=100)
#my views.py
def index(request):
    platform = request.GET.get('platform')
    is_free = request.GET.get('is_free')
    genre = request.GET.get('genre')
    if platform is not None:
        games = games.filter(platform=platform, is_free=is_free, genre=genre)
    return render(request, 'index_games.html', {'games': games})

/*this is index_games*/
{% extends 'base.html' %}

{% block content %}
<h1>Games</h1>
<form method="get" action="/games/">
    <h5>Choose platform</h5>
/*only one choice is allowed here*/
    <select name="platform">
        <option value='PS4'>PS4</option>
        <option value="XBOX">XBOX</option>
        <option value='Switch'>Switch</option>       
    </select>
/*here's the problem, multiple choices should be allowed for is_free and genre
but are not*/
    <h5>Free to Play?</h5><br/>
    <input type="checkbox" name="is_free" value="True" checked>Free<br/>
    <input type="checkbox" name="is_free" value="False">For sale<br/>
    <h5>Genre</h5>
    <input type="checkbox" name="genre" value="Shooter">Shooter<br/>
    <input type="checkbox" name="genre" value="Adventure">Adventure<br/>
    <input type="checkbox" name="genre" value="Strategy">Strategy<br/>
    <hr/>
</form>

<div class="row">
    {% for game in games %}
     <h1>{{game.name}}
    {% endfor %}
</div>
{% endblock %}

Even if multiple checkbox are checked only the results that match the last value checked are returned. I think the problem is caused by giving the same name at different input (e.g: name="genre" repeated for multiple input), so I tried naming those genre1, genre2, genre3 and rewriting like this:

def index(request):
    platform = request.GET.get('platform')
    is_free = request.GET.get('is_free')
    genre = request.GET.get('genre')
    if platform is not None:
        games = games.filter(platform=platform, is_free=is_free, genre=genre1, genre2, genre3)
    return render(request, 'index_games.html', {'games': games})

but I get "unresolved reference" error. Any help or suggestions are greatly appreciated thanks.

JLeno46
  • 1,186
  • 2
  • 14
  • 29

1 Answers1

1

You can get the list of genre from your checkbox

genre = request.GET.getlist('genre')

And then filter like so

games = games.filter(platform=platform, is_free=is_free, genre__in=genre)
crazychukz
  • 676
  • 1
  • 4
  • 10