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

236
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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