title: How to keep user logged in django
body: <p>I made my login in the application. Then I go to the screen that list all the users in my database. When I select an especified user I lost my user that is logged. So, how can I keep my user in all the application?</p>
  
  <p>My views.py that list all the users. In this time, the url shows my user (<a href="http://localhost:8000/os/acesso/consultausuario/9" rel="nofollow noreferrer">http://localhost:8000/os/acesso/consultausuario/9</a>)</p>
  
  <pre><code>def acesso_consultausuario(request, id=None):
  
      instance = get_object_or_404(usuario, id=id)
  
      queryset = usuario.objects.all().order_by('dat_criacao_usuario')
      instancia_usuario_filter = usuariofilter(request.GET, queryset=queryset)
  
      table = usuariotable(instancia_usuario_filter.qs)
  
      RequestConfig(request, paginate={'per_page': 25}).configure(table)
  
      paginator = Paginator(instancia_usuario_filter.qs, 5)
      page = request.GET.get('page')
  
      try:
          response = paginator.page(page)
      except PageNotAnInteger:
          # if page is not an integer, deliver first page
          response = paginator.page(1)
      except EmptyPage:
          # if page is out of range, deliver last pages of results
          response = paginator.page(paginator.num_pages)
  
       context = {
          "instance": instance,
          "filter": instancia_usuario_filter,
          "response": response,
          "table": table,
      }
      return render(request, 'consulta/consulta_usuario.html', context)
  
  
  # Show an especified user that I selected before.
  def acesso_consultausuariodetalhe(request, id=None):
  
      # Instância do usuário que foi selecionado na consulta
      instance = get_object_or_404(usuario, id=id)
  
      form = statususuarioForm(request.POST or None)
  
      if form.is_valid():  # All validation rules pass
  
          instance_status = form.save(commit=False)
          var_status = form.cleaned_data['statususuario']
          instance_status = statususuario.objects.get(desc_statususuario=var_status)
  
  
          # Salva o status atual na tabela logstatususuario
          instance_log = logstatususuario.objects.create(usuario = instance,
                                                          statususuario = instance_status)
          instance_log.save()
  
          # Seleciona o grupo de acesso
          #var_grupo = form.cleaned_data['grupo']
          #instance_grupo = grupo.objects.get(desc_grupo=var_grupo)
  
          # Atualiza o usuário com o novo status
          instance.statususuario = instance_status
          #instance.grupo = instance_grupo
          instance.save()
  
      context = {
          "instance": instance,
          "form": form,
      }
  
      return render(request, 'consultadetalhe/consulta_usuariodetalhe.html', context)
  </code></pre>
  
  <p>My filters.py</p>
  
  <pre><code>    class usuariofilter(django_filters.FilterSet):
  
          dat_criacao_usuario = django_filters.DateFromToRangeFilter()
  
          class Meta:
              model = usuario
              fields = ['id', 'dat_criacao_usuario', 'nom_usuario', 'empresa', 'grupo', 'statususuario']
  
  My tables.py
  
      class usuariotable(tables.Table):
  
          id = tables.Column(verbose_name='ID', attrs={"td": {"align": "left"}})
          dat_criacao_usuario = tables.Column(verbose_name='Data Cadastro'
                                      ,attrs={"td": {"align": "left"}})
          nom_usuario = tables.Column(verbose_name='Nome'
                                        ,attrs={"td": {"align": "left"}})
          empresa = tables.Column(verbose_name='Empresa'
                                        ,attrs={"td": {"align": "left"}})
          grupo = tables.Column(verbose_name='Grupo'
                                        ,attrs={"td": {"align": "left"}})
          statususuario = tables.Column(verbose_name='Status'
                                        ,attrs={"td": {"align": "left"}})
          ver = tables.TemplateColumn('&lt;a href={% url "acesso_consultausuariodetalhe" record.id %} style="color:#e26c2c"&gt;Ver&lt;/a&gt;',
                                      verbose_name = '', )
  
  
          def __init__(self, *args, vb_statusservico="",**kwargs):  #will get the c1_name from where the the class will be called.
              super().__init__(*args, **kwargs)
  
          class Meta:
              model = usuario
              empty_text = ('No results ...')
              fields = (
                          'id',
                          'dat_criacao_usuario',
                          'nom_usuario',
                          'empresa',
                          'grupo',
                          'statususuario',
                          'ver'
                        )
  
          def render_id(self, record):
              return record.id
  </code></pre>