Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

339
Visualizações
Django UserPassesTestMixin, Looping error in get_absolute_url?

Views.py

class ProfileCreate(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = User
    form_class = ProfileForm
    template_name = "profile/profile_new.html"
    slug_field = "username"

    def get_login_url(self):
        return reverse('account_login')

    def test_func(self):
        a = self.request.user.username
        # print (a)
        b = self.kwargs['slug']
        # print (b)
        x = self.get_object().full_name
        # print (x)
        y = ''
        if a == b:
            if x == y:
                return True
        else:
            def get_absolute_url(self):
                return reverse('profile:view_profile', kwargs={"slug": self.request.user.username})


class ProfileEdit(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = User
    form_class = ProfileForm
    template_name = "profile/profile_new.html"
    slug_field = "username"

    def get_login_url(self):
        return reverse('account_login')

    def test_func(self):
        a = self.request.user.username
        #print (a)
        b = self.kwargs['slug']
        #print (b)
        x = self.get_object().full_name
        #print (x)
        y = ''
        if a == b:
            if x != y:
                return True
        else:
            def get_absolute_url(self):
                print("failing")
                return reverse('profile:profile_view', kwargs={"slug": self.request.user.username})

Explanation The LoginRequiredMixin forces the user to login. UserPassesTestMixin checks

  1. if full_name is ''(empty)

  2. if the loggedin user's username is same as the slug field

The test_func() is all correct when it returns True, but in the elseloop ie., if the test condition fails the view should redirect to 'profile_view' but it does not do so. i get the output "failing" in the terminal. only when i do the same inside the get_absolute_url() function it does not work. The complete get_absolute_url() function does not work. What am i doing wrong here?

note: the total process seems to be in a loop here

  1. GET /user1/edit/ HTTP/1.1" 302 0

  2. GET /as/login/?next=/user1/edit/ HTTP/1.1" 302 0

these two URLS keep looping.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda