Listing: Create Filters on the server by search and selection fields - 16

Let's implement the filter options on the server.

We are going to start a new section, once the links are completed in Django there are going to be two voices that would be the filter, that is to say, here we can excuse the redundancy filter our data, we are going to place a filter that searches by the title, that is to say, a text field and by the category, which are the entities that we have, therefore we already have a couple of ways, a couple of fields and you could replicate it if necessary, for example, if you also wanted to create a filter for the type, which we are not going to do because we are creating an online store, a kind of online store, you would simply have to replicate what we are going to do for the category.

Filters on the server

So with that cleared up we're going to start with the server side here in the view defining the fields in a few words the filters are nothing more than conditionals it's that simple we simply ask if such field is if such field is then we apply a new condition a new filter to our query and if it's not well nothing continues Exactly the same that's basically we're going to do something slowly over here and over here what we're going to do would be first ask for a field called share we're going to get it request get obviously it's going to be a get type request Remember that get type requests are used or should be used for query type That is to say for anything that doesn't change the data model here we put share and additionally we're going to ask if this is defined since again it's a completely optional field So if this is not defined we're going to return here we ask if it's defined if it's not defined then we return an empty one with this we have the share we're going with the next one which would be the category it's going to be something similar here I put category ID over here also the field I'm going to call it category ID but here I'm interested in it being an integer to make the query So we would have to convert it to an integer, we are going to do it here: category ID is going to be equal to in category ID there we have already passed it to an integer now we can ask if it is defined or not here we put If category ID is not defined we simply set it to empty and here we are Establishing our data by which we are going to look for the share and the category the next step would be to put here if they are defined to finally apply the filter:

mystore\elements\views.py

def index(request):

   search = request.GET.get('search') if request.GET.get('search') else ''

   category_id = request.GET.get('category_id')
   category_id = int(category_id) if category_id else ''

   elements = Element.objects
   
   if search:
       elements = Element.objects.filter(title__contains=search)
   
   if category_id:
       elements = elements.filter(category_id=category_id)

   elements = elements.filter(type=2).all()

   categories = Category.objects.all()

- Andrés Cruz

En español

This material is part of my complete course and book; You can purchase them from the books and/or courses section, Curso y Libro desarrollo web con Django 5 y Python 3 + integración con Vue 3, Bootstrap y Alpine.js.

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.