I am trying to use Form filters and Pagination together in a view and only one works at one point of time, I am not able to make both work together, Please help if there is a workaround to my problem.
views.py
`
def viewAmc(request):
amc = Amc.objects.all()
paginator = Paginator(amc, 10)
page_obj = paginator.page(request.GET.get('page', '1'))
amcFilter = AmcFilter(request.GET, queryset=amc)
amc = amcFilter.qs#qs means query set
context = {'amc': page_obj,
'amcFilter': amcFilter}
return render(request, 'view_amc.html', context)
` filters.py
`
class AmcFilter(django_filters.FilterSet):
class Meta:
model = Amc
fields = 'amc_type', 'amc_status'
` view_amc.html
`
{% extends 'base.html' %}
{% load static %}
{% block content %}
<br>
<div class="row">
<div class="col-md-8">
<div class="card card-body">
<h5> Annual Maintenance Contract :</h5>
<div class="card card-body">
<form method="get">
{{ amcFilter.form.amc_type.label_tag }}{{ amcFilter.form.amc_type }}
{{ amcFilter.form.amc_status.label_tag }}{{ amcFilter.form.amc_status }}
<button class="btn btn-primary" type="submit">Filter</button>
</form>
</div>
<div class="col">
</div>
<div class="card card-body">
<a class="btn btn-primary btn-sm btn-block" href="{% url 'create_amc' %}">Create New AMC</a>
<table class="table table-sm">
<tr>
<th>Customer</th>
<th>Location</th>
<th>AMC ID</th>
<th>AMC Type</th>
<th>AMC Status</th>
<th>AMC Cost</th>
<th>AMC Start</th>
<th>AMC Expiry</th>
</tr>
{% for items in amc %}
<tr>
<td>{{ items.customer }}</td>
<td>{{ items.location }}</td>
<td>{{ items.name }}</td>
<td>{{ items.amc_type }}</td>
<td>{{ items.amc_status }}</td>
<td>{{ items.amc_cost }}</td>
<td>{{ items.start_date }}</td>
<td>{{ items.end_date }}</td>
{% endfor %}
</tr>
</table>
</div>
<nav><div class="paginationDiv">
<ul class="pagination" >
{% if amc.has_previous %}
<li>
<a href="?page={{amc.previous_page_number}}" aria-label="Previous">
<span aria-hidden="true"><b>Prev</b></span>
</a>
</li>
{% endif %}
{% if amc.paginator.num_pages > 1 %}
{% for p in amc.paginator.page_range %}
<li class="{% if amc.number == p %}active{% endif %}"><a href="?page={{p}}"><b>{{p}}</b></a></li>
{% endfor %}
{% if amc.has_next %}
<li>
<a href="?page={{amc.next_page_number}}" aria-label="Next">
<span aria-hidden="true"><b>Next</b></span>
</a>
</li>
{% endif %}
{% endif %}
</ul>
</div>
</nav>
</div>
</div>
</div>
{% endblock %}
`
Now, Either I can make Paginator to use the object ‘amc’ in the context by passing it to ‘page_obj’ or changing to ‘amc’ will make Form filters to work, cant achieve both together. Please show me a work around for this?