Login to Django

Asked

Viewed 613 times

1

I have a login page, I need to check who’s logging in, Example: if you are responsible for group A, direct to the page with all users of group A, if you are responsible for group B, to the page of users of group B and so on

py.models

class Grupo(models.Model):

    grupo = models.CharField(max_length=255)
    divisao = models.ForeignKey(Divisao)
    responsavel = models.ForeignKey(User)

    @property
    def get_responsavel(self):
        return self.responsavel

    def __unicode__(self):
        return self.grupo


class Perfil(User):
    telefone = models.CharField(max_length=255)
    ramal = models.CharField(max_length=255)
    data_inicio = models.DateField(default=datetime.now, blank=True)
    data_fim = models.DateField(null=True, blank=True)
    e_responsavel = models.BooleanField(default=False)
    vinculo = models.ForeignKey(Vinculo)
    grupos = models.ForeignKey(Grupo)
    divisao = models.ForeignKey(Divisao)

    def __unicode__(self):
        return self.first_name

py views.

def login_view(request):
    print(request.user.is_authenticated())
    title = "Login"
    form = UserLoginForm(request.POST or None)
    if form.is_valid():
        username = form.cleaned_data.get("username")
        password = form.cleaned_data.get("password")
        user = authenticate(username=username,password=password)
        login(request, user)
        print(request.user.is_authenticated())
        return redirect("/cadastro/sucesso")
        return render(request, 'sucesso.html')

        return redirect("/cadastro/sucesso")
    return render(request, 'login.html', {'form':form, 'title':title})

I currently have the login page that redirects to the successful page if you are a user on Jango, I don’t know how to make this responsible filter

1 answer

0


Before you need to answer some questions, the user can be responsible for more than one group? (your template today allows). I’ll assume not, because if he’s responsible for two groups to which of the groups you’d send him?

So considering that the user can only be responsible for a group would look like this:

def login_view(request):
    print(request.user.is_authenticated())
    title = "Login"
    form = UserLoginForm(request.POST or None)
    if form.is_valid():
        username = form.cleaned_data.get("username")
        password = form.cleaned_data.get("password")
        user = authenticate(username=username,password=password)
        # ESSA LINHA PEGA O GRUPO QUE O SEU USUARIO É RESPONSAVEL
        grupo_responsavel = Grupo.objects.filter(responsavel=user)
        if not grupo_responsavel.exists():
          # CASO ELE NÃO SEJA RESPONSAVEL DE NENHUM GRUPO
          # SEU CODIGO AQUI (REDIRECIONAR PARA UMA PAGINA)

          # AQUI VOCÊ TEM TODOS OS PERFILS DO GRUPO QUE O USUARIO QUE ESTÁ LOGANDO É RESPONSAVEL
        usuarios_grupo = Perfil.objects.filte(grupos=grupo_responsavel)
        # PASSANDO A LISTA DE USUARIOS PARA O SEU TEMPLATE
        return render(request, 'pag.html', {'usuarios':usuarios_grupo, 'title':title})
    return render(request, 'login.html', {'form':form, 'title':title})
  • Thanks, solved!! But answering your question, the user can be responsible for more than one group

  • A question, I do not want this list of users to be in the login view, I want to be in the list view, how do I compare with the logged in user like you did there?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.