Login in Django

1

I have a login page, I need to check who is logging in, Example: if you are in charge of group A, go to the page with all the users in group A, if you are in charge of group B, to page of users in group B and so on

models.py

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

views.py

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})

Currently I have the login page that redirects to the success page if it is a user in django, I do not know how to do this filter of responsible

    
asked by anonymous 20.09.2017 / 16:05

1 answer

0

Before you need to answer some questions, can the user be responsible for more than one group? (your model today allows). I suppose not, because if he is responsible for two groups to which of the groups you would 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})
    
20.09.2017 / 22:25