Listing: Setting values ​​for server filter in Form in Django - 18

Video thumbnail

The next step would be to compose the values ​​that we have here in the view, meaning the category ID and the field called share. Then it is really very simple, for example for this one the only thing we have to do would be to place the value in the form field:

<input type="text" name="search" class="form-control" placeholder="Search" value="{{search}}">

And for the category an If and this would be practically everything in a few words this conditional will place the option as selected if the category ID corresponds to the category that is iterating at a given time:

<div class="col-auto">
    <select name="category_id" class="form-select">
        <option value="">Categories</option>
        {% for c in categories %}
        <option 
        {% if category_id == c.id %} selected {% endif %}
        value={{c.id}}>
            {{c.title}}
        </option>
        {% endfor %}
    </select>
</div>

And on the server:

mystore\elements\views.py

def index(request):
   paginator = Paginator(elements,10)
   page_number = request.GET.get('page')

   return render(request,'elements/index.html', {'elements': paginator.get_page(page_number), 
                                                 'search' : search, 
                                                 'category_id': category_id,
                                                 'categories': categories
                                                 })

I agree to receive announcements of interest about this Blog.

Let's set the values ​​in the server filter.

- Andrés Cruz

En español