title: Django view doesn't contain a HTTP_REFERRER header
body: <p>I'm testing a view that is accessing <code>request.META</code> where I want to get the <code>HTTP_REFERRER</code> header. The purpose being that I want to redirect to a previous page in case the queryset within the <em>search_list</em> view returns empty. A referring page happens to contain a form which is submitted through a <code>GET</code> request. The cleaned data is then passed to <code>search_list</code> as a query string.</p>
  
  <p>When testing the view, all test assertions pass. However when I <code>python manage.py runserver</code> and simulate the test case I get an error: <code>KeyError at /minerals/search; 'HTTP_REFERRER'</code>. I'm trying to understand why I'm getting a KeyError when running the server?</p>
  
  <p>I tried using <em>HTTP_REFERER</em> and <em>HTTP_REFERRER</em> for keys but both fail.</p>
  
  <p>Also noting this:</p>
  
  <pre><code>Internal Server Error: /minerals/search/
  "GET /minerals/search/?query=Kryptonite HTTP/1.1" 500
  </code></pre>
  
  <p><strong>test_views.py</strong></p>
  
  <pre><code>class SearchFormResults(TestCase):
  
      def test_user_search_query_fail(self):
          response = self.client.get(
              reverse("minerals:search_list"), data={'query': 'Kryptonite'},
              HTTP_REFERRER=reverse(
                  "minerals:letter_list", kwargs={'query': 'N'}
              ), follow=True
          )
          self.assertRedirects(response, reverse(
                  "minerals:letter_list", kwargs={'query': 'N'}
              )
          )
          self.assertTemplateUsed("minerals/list.html")
          self.assertEqual(response.status_code, 200)
          self.assertContains(response, "No minerals exist with the provided search.")
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>def search_list(request):
      search_query = request.GET.get('query')
      matched_minerals = Mineral.objects.filter(
          Q(name__icontains=search_query)|
          Q(image_caption__icontains=search_query)|
          Q(category__icontains=search_query)|
          Q(formula__icontains=search_query)|
          Q(strunz_classification__icontains=search_query)|
          Q(crystal_system__icontains=search_query)|
          Q(unit_cell__icontains=search_query)|
          Q(color__icontains=search_query)|
          Q(crystal_symmetry__icontains=search_query)|
          Q(cleavage__icontains=search_query)|
          Q(mohs_scale_hardness__icontains=search_query)|
          Q(luster__icontains=search_query)|
          Q(streak__icontains=search_query)|
          Q(diaphaneity__icontains=search_query)|
          Q(optical_properties__icontains=search_query)|
          Q(group__icontains=search_query)
      )
  
      if not matched_minerals:
          messages.info(request, "No minerals exist with the provided search.")
          previous_page = request.META["HTTP_REFERRER"]
          return HttpResponseRedirect(previous_page)
      else:
          search_form = SearchForm()
          return render(
              request, 'minerals/list.html', 
              {'minerals': matched_minerals, 'form': search_form}
          )
  
  </code></pre>