Search code examples
djangodjango-filter

All my rows are still visible eventhough i am filtering


THis my first time using the django filter and i am trying to filter my all my records are still show even do my url says it is filtering the my records- any help will be appreciated

This my filter File

class Clientsearch(django_filters.FilterSet):
    class Meta:
        model = client
        fields ='__all__'
        exclude = ['Companyemail','Telephone','address','Postcode','RegNom','UTRno','PAYE','VAT','pensionusername','pensionpassword','companyfilling','authenticationcode','gatewayonlineuserid','gatewayonlinepassword',
    'finacialdate',
    'confirmationdate',
    'agreedfees','updated_at','created_at','datejoined']

This my view.py

def manage_client(request):
    clients=client.objects.all()
    myFilter = Clientsearch(request.GET,queryset = clients)
    context = {'myFilter':myFilter,'clients':clients}
    clients = myFilter.qs
    return render(request,"admintemplate/manage_client_template.html",context)

This the Html Code for my Table - All the rows are still show but i can see in the url it is filtering

<br>
<div class="row">
    <div class="col">
        <div class="card card-body">

            <form method ="get">
        {{myFilter.form}}
        <button class="btn btn-primary" type ="submit">Search</button>
      </form>

        </div>
    </div>
    
</div>
<br>

<div class="row">
    <div class="col-md">
        <div class="card card-body">
            <table class="table table-sm">
                  <tr>
                    <th>ID</th>
                    <th>Company Name</th>
                    <th>Company Email</th>
                    <th>Sector</th>
                    <th>Employee Assigned</th>
                    <th>Edit</th>
                    <th>Delete</th>
                  </tr>

                  {% for client in clients %}
                    <tr>
                      <td>{{client.id}}</td>
                      <td>{{client.Companyname}}</td>
                      <td>{{client.Companyemail}}</td>
                      <td>{{client.sector}}</td>
                      <td>{{client.username}}</td>
                      <td><a href="/edit_client/{{ client.id}}" class ="btn btn-success">Edit</a></td>
                      <td><a href="/delete_client/{{ client.id}}" class ="btn btn-success">delete</a></td>
                    {% endfor %}  
                </table>
              </div>
            </div>
          </div>

Solution

  • You are using the queryset you passed clients = client.objects.all(), use the filters queryset which you can access using myFilter.qs, i.e in your template the loop will be:

    {% for client in myFilter.qs %}