I have a Django form that takes input values from users. The values are then used in making query to a table ResourceBase, which finally returns a list of filtered results.
Since the results might be a long list, I added a pagination function with "Prev" and "Next" buttons. My problem is that when I click "Prev" or "Next" button, the form gets restored into default values. And all returned results are all gone. How do I prevent this from happening?
I think the form gets reset because of "form1 = QueryForm()" when a request is not "POST". However I just have difficulty coming up with a neat solution since I'm new to Django and web dev.
In views.py:
def search(request):
    if request.method == "POST":
        form1 = QueryForm(data=request.POST)
        layer_dict = []
        if form1.is_valid():
            inp_ct = form1.cleaned_data['country']
            q1 = ResourceBase.objects.filter(country_name__iexact=inp_ct)
            for layer in q1:
                down_url = 'xxxxxxx'.format(layer.title)
                view_url = 'xxxxxxx'.format(layer.title)
                layer_dict.append((layer.title, down_url, view_url))
            layer_dict = sorted(layer_dict, key = lambda x:x[0])
            paginator = Paginator(layer_dict, 10)
            page = request.GET.get('page', 1)
            try:
                layers = paginator.page(page)
            except PageNotAnInteger:
                # If page is not an integer, deliver first page.
                layers = paginator.page(1)
            except EmptyPage:
                # If page is out of range (e.g. 9999), deliver last page of results.
                layers = paginator.page(paginator.num_pages)
            context = {'form1': form1, 'layers': layers}
    else:
        form1 = QueryForm()
        context = {'form1': form1}
    return render(request, 'my_app/search.html', context)
In search.html:
<br />
<h3>Pagination Test</h3>
<br /><br/>
<div class="row">
    <div class="col-md-4">
        <form method="POST">
            {% csrf_token %}
              <div class="form-controls">
                {{ form1|as_bootstrap }}
              </div>
            <button class="btn btn-primary" type="submit" style="float: right;" title = "Click to search" ><i class="fa fa-search"></i></button>
        </form>
        <form method="GET">
            <button class="btn btn-primary" type="submit" value="Reset" name="Reset" title="Reset all choices">Reset</button>
        </form>
    </div>
</div>
{% if layers %}
<div class="row">
    <div class="col-md-8">
        <div id = "search_results" >
            <table class="table table-hover">
              <thead>
                <tr>
                  <th scope="col">Select</th>
                  <th scope="col">Layer Name</th>
                  <th scope="col">Download</th>
                  <th scope="col">View Layer</th>
                </tr>
              </thead>
              <tbody>
                {% for layer in layers %}
                <tr>
                  <td><input class= messageCheckbox type="checkbox" name="checks" value="{{layer.1}}"/></td>
                  <td>{{layer.0}}</td>
                  <td><a href="{{layer.1}}" target="_blank"> Download Layer </a></td>
                  <td><input class="btn btn-primary" onclick="window.open('{{layer.2}}')" id="view" type="button" name="view" value="View"></td>
                </tr>
                {% endfor %}
                <tr>
                    <td><input type="checkbox" onClick="toggle(this, 'checks')"/> Select All</td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
              </tbody>
            </table>
            <button class="btn btn-primary" type="button" name="download" style="float: left;" onClick= "open_all_links();">Download Selected</button>
        </div>
     <div class="a_pagination" align="right">
            <span class="step-links">
                {% if layers.has_previous %}
                    <a class="btn btn-primary btn-sm" name="prev_page" href="?page={{ layers.previous_page_number }}" role="button">Prev.</a>
                {% endif %}
                <span class="current" style ="color:#2C689C;font-size:16px;padding:8px;">
                    page {{ layers.number }} of {{ layers.paginator.num_pages }}
                </span>
                {% if layers.has_next %}
                    <a class= "btn btn-primary btn-sm"  href="?page={{ layers.next_page_number }}" role="button">Next</a>
                {% endif %}
            </span>
        </div>
    </div>
</div>
{% endif %}
<script type="text/javascript" >
.......
</script>
 
     
     
    