Здравствуйте. У меня проблема с фильтрацией по id категориям, id жанрам и годам. Сама фильтрация работает, но если выбрать 'All' или не передать года то появляется ошибка. Cвязана она с тем что в url передает
/games/filter/?genre=All&platfrom=All&year1=&year2=
models:
class Platform(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True)
def __str__(self):
return self.name
class Genre(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
def __str__(self):
return self.name
class Game(models.Model):
...
platform = models.ManyToManyField(Platform)
genre = models.ManyToManyField(Genre)
year = models.DateField(default=date.today)
Views:
def games_list(request):
object_list = Game.objects.all()
genre_list = Genre.objects.all()
platform_list = Platform.objects.all()
paginator = Paginator(object_list, 10)
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request, 'blog_t/games.html', {'page': page, 'posts': posts, 'genre_list': genre_list, 'platform_list': platform_list,})
def Filter_games(request):
object_list = Game.objects.filter(
Q(genre__id=request.GET.get('genre'))|
Q(platform__id = request.GET.get('platform'))|
Q(year__year__gte=request.GET.get('year1'), year__year__lte=request.GET.get('year2'))
)
genre_list = Genre.objects.all()
platform_list = Platform.objects.all()
paginator = Paginator(object_list, 10)
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request, 'blog_t/games.html', {'page': page, 'posts': posts, 'genre_list': genre_list, 'platform_list': platform_list})
urls
path('games/', views.games_list, name='games_list'),
path('games/filter/', views.Filter_games, name='filter_games'),
Html
<form action="{% url 'filter_games' %}" method="GET">
<h5>Genre: <select class="form-select" aria-label="Default select example" name='genre'>
<option>All</option>
{% for genre in genre_list %}
<option name='genre' value="{{ genre.id }}">{{ genre.name }}</option>
{% endfor %}
</select></h5><br>
<h5>Platform: <select class="form-select" aria-label="Default select example" name='platfrom'>
<option>All</option>
{% for platform in platform_list %}
<option name='platform' value="{{ platform.id }}">{{ platform.name }}</option>
{% endfor %}
</select></h5><br>
<div class="mb-3">
<h5>Start year</h5>
<input type="year1" name="year1" class="form-control" id="exampleFormControlInput1" placeholder="year">
</div>
<div class="mb-3">
<h5>End year</h5>
<input type="year2" name="year2" class="form-control" id="exampleFormControlInput1" placeholder="year">
</div>
<button class="btn btn-primary">Search</button>
</form>
Как сделать если при выборе "All" в url ничего не передавал и так же с годами