title: How to clear entry in Django session
body: <p>I have a search form that I use to then list the objects with a paginated ListView. For the pagination to work, I need to keep the form fields in the list view, which I did so far using session. In the view of the form, I want to clear the values of the potential previous searches. So I have something like this:</p>
  
  <pre><code>    class SearchForm( forms.Form ):
        name = forms.CharField()
  
      def SearchView( request ):
          form = SearchForm() 
          &lt;CLEAR SESSION ENTRY HERE&gt;
          return render( request, 'search.html', {
              'form': form,
          } )
  
  
  
      class listView( ListView ):
        model = myModel
        template_name = "list.html"
        paginate_by = 10
        form = None
  
  
        def get( self, request, *args, **kwargs ):
            form = SearchForm( request.GET )  # A form bound to the POST data
  
          if form.is_valid():
  
            name = form.cleaned_data['name']
            if name:
              self.request.session.setdefault( 'search_form', {} )['name'] = name
  
          return super( orderList_view, self ).get( request, *args, **kwargs )
  
        def get_queryset( self ):
          searchFilters = self.request.session.get( 'search_form', {} )
          queryset = myModel.objects.all()
          if 'name' in  searchFilters:
            queryset = queryset.filter( mymodel__name__iexact = searchFilters.get( 'name' ) )
          return queryset
  </code></pre>
  
  <p>My problem is clearing the entry.
  If I do it that way</p>
  
  <pre><code>    request.session.pop( 'search_form', None )
  </code></pre>
  
  <p>everything works perfectly. </p>
  
  <p>However if I do it that way</p>
  
  <pre><code>    request.session['search_form'] = dict()
  </code></pre>
  
  <p>the session seems to be cleared when I go on the next page. The 1st page contains only entries that match my search, and the total number of page is correct. But when I click "next", it then returns all the entries, the searchFilters is empty. </p>
  
  <p>I don't understand why, since  doing session.setdefault( 'search_form', {}) should make the two clearing methods the same.Can someone explain me why one works and not the other, and why is my session "cleared" in the second case?</p>
  
  <p>Btw, I use a dictionary search_form inside the session because in reality I have several search fields, and it's "cleaner" to keep them together</p>