Id from email entered

1

I have a form with only the email label for the user to fill out. If the email already exists I want to redirect it to the edit page. How do I get the user id through the email he typed? I did a test as shown below with id 14 and is going 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")
    
asked by anonymous 03.10.2017 / 17:16

1 answer

0

You can search through the query that is already being done in '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')
    
03.10.2017 / 17:53