Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
KeyError at /login u'message'
def loginUser(request):
    if request.method != 'POST':
        return redirect('/')
    attempt = User.objects.validateUser(request.POST)
    if attempt['status'] == True:
        request.session['user_id'] = attempt['user'].id
        return redirect('/books')
    else:
        messages.add_message(request, messages.ERROR, attempt['message'], extra_tags="login")
        return redirect('/')

KeyError at /login u'message' Request Method: POST Request URL: http://127.0.0.1:8000/login Django Version: 1.11 Exception Type: KeyError Exception Value:
u'message'

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

It says that the dictionary attempt does not have the key message. Here you get the dictionary:

attempt = User.objects.validateUser(request.POST)

At this point inspect the value of attempt (print in console, or assert False and deal interactively in browser). If it does not have the value for key message you may provide here (validateUser method) or, let it be optional, and change the way you access the value at:

messages.add_message(request, messages.ERROR, attempt.get('message', 'default_msg'), extra_tags="login")

Note, the changes is from:

attempt.['message']

to

attempt.get('message', 'default_msg')
over 4 years ago · Santiago Trujillo Denunciar

0

You can use these two ways.

# either
try:
    message = attempt['message']
except KeyError:
    message = 'default message or your own message'
# or
message = attempt.get('message', 'default message you need to set it yourself')

# after that just use the message variable
messages.add_message(request, messages.ERROR, message, extra_tags="login")
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda