Id from the inserted email

Asked

Viewed 45 times

1

I have a form with only the email label for the user to fill. If the email already exists I want to redirect it to the edit page. How do I get the user id from the email he typed? I did a test as shown below with id 14 and is passing to the right page, I just have to figure out how to get the id.

def email(request):
form = EmailForm(request.POST or None)
if form.is_valid():
    email = form.cleaned_data.get("email")
    try:
        profile = Perfil.objects.get(email=email, is_staff=False)
        print("email ja existe e nao staff")
        return redirect("cadastro:editar", id=profile.id)
    except:
        if not Perfil.objects.filter(email=email).exists():
            print("novo usuario")
            return redirect("/cadastro/cadastro")
        else:
            print("já entra no sistema")
            return redirect("/cadastro/login")

1 answer

0


You can search through the query that is already being done in the 'if', like this:

def email(request):
    form = EmailForm(request.POST or None)
    if form.is_valid():
        email = form.cleaned_data.get("email")

        try:
            profile = Perfil.objects.get(email=email, is_staff=False)
            print("email ja existe e nao staff")
            return redirect("cadastro:editar", id=profile.id)
        except Perfil.DoesNotExist:
            print('email nao existe')
  • The filter returns a Queryset, there is no way to get the id using filter, gives the following error: 'Queryset' Object has in attribute 'id'

  • Sorry. I changed the code above. I switched . filter with . get

  • Really. get returns an Exception if the object does not exist. I changed the above code one more time to adjust the Exception.

  • I got Marcos! Thanks for the help! I changed the code the way it was!!

Browser other questions tagged

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