简体   繁体   English

form [“ x”]引发KeyError

[英]form[“x”] throws a KeyError

I'm working on an assignment and we are to create a HTML order form then execute the info by python to create a second customer receipt. 我正在处理一项任务,我们将创建一个HTML订单表格,然后通过python执行信息以创建第二个客户收据。

Here is the error msg: 这是错误消息:

Traceback (most recent call last):
  File "F:\Assignment 3\page.py", line 17, in <module>
    print "<p>Customer Name:", form["custName"].value, "</p>"
  File "C:\Python27\lib\cgi.py", line 540, in __getitem__
    raise KeyError, key
KeyError: 'custName'

THE HTML: HTML:

<form action="page.py">

<div class="personalinfohead">
    <p>Personal Information:</p>
</div>

<div class="personalinfo">
    <div>Full name:
        <input type="text" name="custName" size="20" />
    </div>

    <div>Email address:
        <input type="text" name="custEmail"  size="50" />
    </div>

    <div>Street address:
        <input type="text" name="custAdd"  size="50" />
    </div>

    <div>City:
        <input type="text" name="custCity"  size="15" />
    </div>

    <div>Province:
        <input type="text" name="custProv"  size="2" maxlength="2" />
    </div>

    <div>Postal code:
        <input type="text" name="custPostal"  size="6" maxlength="6" />
    </div>
</div>

PYTHON: 蟒蛇:

import cgi
form = cgi.FieldStorage()

# print HTTP/HTML header stuff
print """Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
<title>Order Form</title>
</head><body>
"""

# print HTML body using form data
print "<h1>Kintoro Japanese Bar &amp; Restaurant</h1>"
print "<h2>Customer Reciept</h2>"
print "<p>Customer Name:", form["custName"].value, "</p>"
print "<p>Customer Email Address:", form["custEmail"].value, "</p>"
print "<h2>Customer Address:</h2>"
print "<p>Street:", form["custAdd"].value, "</p>"
print "<p>City:", form["custCity"].value, "</p>"
print "<p>Province:", form["custProv"].value, "</p>"
print "<p>Postal Code:", form["custPostal"].value, "</p>"

Jack is correct, but there is a backstory and quick/dirty testing method for the future students. 杰克是正确的,但对于未来的学生,有一种背景知识和快速/肮脏的测试方法。

Explanation first: 首先说明:

Originally, your KeyError said there was no Key with that name. 最初,您的KeyError表示没有使用该名称的Key After you've implicitly stated a key (the first part of a dict), it was then missing the Value for said key. 在隐式声明了一个键(字典的第一部分)之后,它便缺少了该键的

Dictionaries are a key-value pair, so both would need to be implicitly stated at the start of the script (in the scope the previous and following debugging-with-ease methods). 字典是一个键-值对,因此都需要在脚本开始时隐式声明两者(在作用域中,之前和之后的易用调试方法)。

Slowing down the actions helps obtain clearer understanding; 放慢动作,有助于更清楚地理解;

Since this script is a fully loaded CGI that is told to start and finish by declaring variables for each key-value pair, you are seeing the end result of which - where python feeds text data to CGI, CGI then accepts and interprets said text, and gives some sort of response back to python (valid or not!), python can only give you the results of the results. 由于此脚本是完全加载的CGI,通过声明每个键值对的变量来开始和结束,因此您将看到最终结果-python将文本数据馈送到CGI,然后CGI接受并解释所述文本,并给python某种响应(有效或无效!),python只能给您结果的结果。 Thusly, this error looks different than your standard (and excellent I may add) non-cgi / console error with followable tracebacks. 因此,此错误看上去与您的标准(且可能要补充的)非cgi /控制台错误有所不同,并具有可追溯的跟踪。

A quick/dirty test method: 快速/肮脏的测试方法:

Implicitly state an exact key-value pair before telling CGI to pass it back to python to use: 在告诉CGI将其传递回python使用之前,隐式声明一个确切的键值对:

custName = { 'Name': 'John Smith' }

One would need to declare a default setting for each dict mentioned as values to have a fully operational loaded script ready to use, but the hint here is that custName would no longer present the error, but it would then complain about your next missing key-value pair. 一个人需要为每个字典声明一个默认设置,将其作为值使用,以准备使用一个完全可操作的已加载脚本,但是这里的提示是custName将不再显示错误,但是它将抱怨您的下一个缺少键-价值对。

Yeah, long answer and past classtime - I know. 是的,答案很长,上课时间很长-我知道。 Hopefully, however, this will assist to understand the several parts of a 'single' issue than to solve it once for only a select few people. 但是,希望这将有助于理解“单个”问题的多个部分,而不是仅针对少数几个人解决一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM