samedi 26 janvier 2019

Avoiding "DoesNotExist at /"

I made a log in view, which works perfectly. The issue is that I want to avoid the Django's default template message:

DoesNotExist at /
 Usuario matching query does not exist.
  Request Method:   POST
  Request URL:  http://localhost:8000/
  Django Version:   2.1.5
  Exception Type:   DoesNotExist
  Exception Value: Usuario matching query does not exist.

Instead of that I'm trying to send my own login error message, my code is like this:

def login(request):
    if request.method == "POST":
        print('Form is post.')
        form = LoginForm(request.POST)
        if form.is_valid():
            print('Form is valid.')
            loginUser = form.cleaned_data.get('nombreUsuario')
            loginPass = form.cleaned_data.get('password')
            loginEmp = form.cleaned_data.get('nombreEmpresa')
            encontroUsuario = Usuario.objects.get(nombre=loginUser, password=loginPass)
            encontroEmpresa = Empresa.objects.get(nombre=loginEmp)
            if encontroUsuario and encontroEmpresa:
                return HttpResponse('Logueado con éxito.')
            if encontroUsuario.DoesNotExist or encontroEmpresa.DoesNotExist:
                return render(request, "loginError.html", {"form": form})
    else:
        form = LoginForm
        return render(request, "login.html", {"form": form})




Aucun commentaire:

Enregistrer un commentaire