Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!