N
N
N_-_N2021-07-09 14:53:41
Django
N_-_N, 2021-07-09 14:53:41

Filtering problem?

Hello. I have a problem with filtering by category id, genre id and year. The filtering itself works, but if you select 'All' or do not pass the year, an error appears. It is connected with the fact that the url passes
/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>

How to do if when choosing "All" in the url did not pass anything and the same over the years

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2021-07-09
@alternativshik

"How to do if when choosing "All" in the url did not pass anything and the same over the years" well, you remove your ALL in the template. Well, or in the condition, add that if All then the request is different.
In general, the code is hellishly written.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question