Jump to content


Photo

Ch. 7, P. 246


  • Please log in to reply
5 replies to this topic

#1 earth.terminal

earth.terminal

    New Member

  • Members
  • Pip
  • 1 posts

Posted 11 August 2011 - 10:27 AM

Hi—I am on a Mac, working on creating the generate_timing_data.py. When I run the program I am receiving a "KeyError" on 'which_athlete' in athlete_name = form_data['which_athlete'].value My code looks as follows:

#! /usr/local/bin/python3

import cgi
import yate
import athletemodel

athletes = athletemodel.get_from_store()

form_data = cgi.FieldStorage()
athlete_name = form_data["which_athlete"].value

print (yate.start_response())
print(yate.include_header("Coach Kelly's Timing Data"))

print(yate.header("Athlete:"+athlete_name, "DOB:"+ athlete[athlete_name].dob+ "."))

print(yate.para("The top times for this athlete are:"))
print(yate.u_list(athletes[athlete_name].top3() ))

print(yate.include_footer({"Home": "/index.html", "Select another athlete": "generate_list.py"}))



Do you know what may be the issue?

#2 paulbarry

paulbarry

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 307 posts

Posted 08 September 2011 - 12:34 AM

Make sure your code can see your data files. --Paul.

#3 Hanan-Natan

Hanan-Natan

    New Member

  • Members
  • Pip
  • 4 posts

Posted 17 September 2011 - 10:44 PM

Make sure your code can see your data files. --Paul.


I would like to know how you have come to.the conclusion that the problem relates to the data files, becasue the key-error comes from the cgi module that relates to the previus page, and this page (generate_timing_data.py) isn't suppose to relate to the data files.
Thanks.

#4 paulbarry

paulbarry

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 307 posts

Posted 18 September 2011 - 08:27 AM

I know it sounds strange, but if the CGI/web-server can't find the data files it fails "silently" and then when the later code tries to create the dictionary it ends up empty... When you try to access a non-existent data item in the dictionary you get a "key-error". Check the logging information on the screen of the web-server... if will say "File Not Found" somewhere... and this indicates that the CGI can't find the data it needs.

--Paul.

#5 Hanan-Natan

Hanan-Natan

    New Member

  • Members
  • Pip
  • 4 posts

Posted 18 September 2011 - 09:25 AM

Hi,

The solution to the question above is, probably because you have copied the code from the source code that you have downloaded from the web, and there is a mistake in the suppose to be generate_list.py file, in line 13 (where you print the radion button), you have to correct the syntax of the first argument that supplied to the radio_button function, instead of 'where_thlete' sould be 'where_athlete'.

i am recommend to you to check again the above code that you have posted here, becasuse there is a call to a variable that isn't defind, although it isn't relate to the error code that you have got, you will encounter it at the future.

hope it solve your problem.

#6 t.waner

t.waner

    New Member

  • Members
  • Pip
  • 1 posts

Posted 02 September 2013 - 04:47 PM

I ran into this same issue make sure that the line:
print(yate.radio_button("which_athlete", athletes[i].name))
and
athlete_name = form_data["which_athlete"].value

reference 'which_athlete' with the same spelling.

You can also view source on the page with the radio buttons to verify their value:
<input type="radio" name="which_athlete" value="James Lee"> James Lee<br />




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users