AttributeError:' NoneType'对象没有属性'替换' (蟒)

时间:2017-03-29 13:44:30

标签: python html

enter image description here

http://cs1.ucc.ie/~adc2/cgi-bin/lab7/index.html 您可以通过向任何一个框中输入任何内容来查看错误,不必全部,任何帮助都会很棒 ,我将在此之后发送代码

from cgitb import enable  
enable()

from cgi import FieldStorage,escape

print('Content-Type: text/html')  
print()


actor=''

genre=''

theyear=''

director=''

mood=''

result=''


form_data= FieldStorage()  
if len(form_data) != 0:  

    try:    
        actor=escape(form_data.getfirst('actor'))
        genre=escape(form_data.getfirst('genre'))
        theyear=escape(form_data.getfirst('theyear'))
        director=escape(form_data.getfirst('director'))
        mood= escape(form_data.getfirst('mood'))
        connection = db.connect('####', '###', '####', '###')
        cursor = connection.cursor(db.cursors.DictCursor)
        cursor.execute("""SELECT title
                          FROM films
                          WHERE (actor = '%s')
                          OR (actor='%s' AND genre='%s')
                          OR (actor='%s' AND genre='%s' AND theyear='%i')
                          OR (actor='%s' AND genre='%s' AND theyear='%i' AND director='%s')
                          OR (actor='%s' AND genre='%s' AND theyear='%i' AND director='%s' AND mood='%s') % (actor, actor,genre, actor,genre,theyear, actor,genre,theyear,director,actor,genre,theyear,director,mood))
                          """)
        result = """<table>
                    <tr><th>Your movie!</th></tr>
                    <tr><th></th></tr>"""
        for row in cursor.fetchall():
            result+= '<tr><td>%s</td></tr>' ,(row['title'])
        result+= '</table>'
        cursor.close()
        connection.close()




    except db.Error:
        result = '<p>Sorry! We are currently experiencing technical difficulties.</p>'

2 个答案:

答案 0 :(得分:0)

您的<input>名为year,但您尝试运行escape(form_data.getfirst('theyear'))。如果没有相应的表单值,则getfirst会返回Noneescape会失败一个None。出于类似的原因,您需要更好地处理可选字段,例如Willem在评论中所说的内容。

答案 1 :(得分:0)

根据错误代码:

/users/2020/adc2/public_html/cgi-bin/lab7/index.py in ()
     24     try:
     25         actor=escape(form_data.getfirst('actor'))
=>   26         genre=escape(form_data.getfirst('genre'))
     27         theyear=escape(form_data.getfirst('theyear'))
     28         director=escape(form_data.getfirst('director'))
genre = '', escape = <function escape>, form_data = FieldStorage(None, None, [MiniFieldStorage('actor', 'i')]), form_data.getfirst = <bound method FieldStorage.getfirst of FieldStorage(None, None, [MiniFieldStorage('actor', 'i')])>
 /usr/local/lib/python3.4/cgi.py in escape(s=None, quote=None)
   1038     warn("cgi.escape is deprecated, use html.escape instead",
   1039          DeprecationWarning, stacklevel=2)
=> 1040     s = s.replace("&", "&amp;") # Must be done first!
   1041     s = s.replace("<", "&lt;")
   1042     s = s.replace(">", "&gt;")
s = None, s.replace undefined

escape()似乎将None作为参数。根据给定的代码片段,Escape()在内部直接使用replace()。因此,快速修复将确保您不在转义方法中提供None,但可能是空字符串。

my_non_none_value = form_data.getfirst('actor') if form_data.getfirst('actor') else ""
bla = escape(my_non_none_value)

长版本:

my_non_none_value = form_data.getfirst('actor')
if my_non_none_value is None:
    my_non_none_value = ""
bla = escape(my_non_none_value)

旁注:cgi中的escape()已弃用,请改用html.escape()。