title: dynamic pagination with django
body: <p>Okay so this is first time using pagination with Django and I am trying to prevent it from reloading my view on each page turn.</p>
  
  <p>I'm handling the pagination in the view like this:</p>
  
  <pre><code>page = request.GET.get('page', 1)
  print page
  paginator = Paginator(list(od.iteritems())[:24], 12)
  
  try:
      data = paginator.page(page)
  except PageNotAnInteger:
      data = paginator.page(1)
  except EmptyPage:
      data = paginator.page(paginator.num_pages)
  
  print data
  
  save_query_form = SaveQueryForm(request.POST or None)
  #if request.method == 'POST':
  if save_query_form.is_valid():
      profile = save_query_form.save(commit=False)
      profile.user = request.user
      profile.save()
  
  context = {
      "title":"Search",
      'data': data,#list(od.iteritems()),
      'tools': od_tools.iteritems(),
      'methods': od_methods.iteritems(),
      'data4': od_data.iteritems(),
      'search_phrase': " ".join(instanceValuesString),
      'json_dump': js_data,
      'form': save_query_form,
  }
  return render(request, 'results.html', context)
  </code></pre>
  
  <p>and the pagination is handled in the html:</p>
  
  <pre><code>{% if data.has_other_pages %}
      &lt;div id='page-slide'&gt;
          &lt;ul class="pagination" start='$offset'&gt;
              {% if data.has_previous %}
                  &lt;li&gt;&lt;a href="?page={{ data.previous_page_number }}"&gt;&amp;laquo;&lt;/a&gt;&lt;/li&gt;
              {% else %}
                  &lt;li class="disabled"&gt;&lt;span&gt;&amp;laquo;&lt;/span&gt;&lt;/li&gt;
              {% endif %}
              {% for i in data.paginator.page_range %}
                  {% if data.number == i %}
                      &lt;li class="active"&gt;&lt;span&gt;{{ i }} &lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
                  {% else %}
                      &lt;li&gt;&lt;a href="?page={{ i }}"&gt;{{ i }}&lt;/a&gt;&lt;/li&gt;
                  {% endif %}
              {% endfor %}
              {% if data.has_next %}
                  &lt;li&gt;&lt;a href="?page={{ data.next_page_number }}"&gt;&amp;raquo;&lt;/a&gt;&lt;/li&gt;
              {% else %}
                  &lt;li class="disabled"&gt;&lt;span&gt;&amp;raquo;&lt;/span&gt;&lt;/li&gt;
              {% endif %}
          &lt;/ul&gt;
      &lt;/div&gt;
  {% endif %} 
  </code></pre>
  
  <p>The issue that I am having is that whenever I switch to another page my entire view will run again and the data will does not reflect the original search query and instead defaults to an empty query.  </p>
  
  <p>I was wondering if there is a simple way to either handle pagination dynamically or prevent the page reload when toggling between pages?</p>
  
  <p>Any help is appreciated, thanks.</p>
  
  <p>Update Search Form:</p>
  
  <pre><code>&lt;form action="{% url 'results-view' %}" method="POST" class="autocomplete-me ui-widget" id="myform" &gt;
      {% csrf_token %}
  
      &lt;div class="ui-widget" style="text-align:center;"&gt;
          &lt;input type="text" id="id_q" name="q" placeholder="{{ search_phrase }}"&gt;
          &lt;br&gt;&lt;/br&gt;
          &lt;div style="text-align:center;" id='adjust-button'&gt;
              &lt;input type='submit' class='btn btn-secondary btn-lg' id ='search-btn' value='Search'/&gt;         
              &lt;a class='btn btn-secondary btn-lg' id ='clear-btn' href="{% url 'inital' %}"&gt;Clear&lt;/a&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>