【问题标题】:AttributeError: 'NoneType' object has no attribute 'replace' (python)AttributeError:“NoneType”对象没有“替换”属性(python)
【发布时间】:2017-03-29 13:44:29
【问题描述】:

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>'

【问题讨论】:

  • 嗯,关于错误还有什么不清楚的地方:你打电话给replace,所以你可能假设它是str,但它是None。所以我认为输入无法通过 API 正确运行。
  • 好的,谢谢,我已经添加了代码
  • 我没有在该代码中看到对 str.replace() 的调用。请在您的问题中提供完整的回溯。
  • 这是因为你没有提供 genre 在这种情况下它是None。因此,您应该将 form_data.getfirst('genre') 替换为 form_data.getfirst('genre') or '' (并使用所有类似的行)
  • result+= '&lt;tr&gt;&lt;td&gt;%s&lt;/td&gt;&lt;/tr&gt;' ,(row['title']) 你的意思是result+= '&lt;tr&gt;&lt;td&gt;%s&lt;/td&gt;&lt;/tr&gt;' % (row['title'])(将逗号替换为percent)?

标签: python html


【解决方案1】:

您的&lt;input&gt; 被命名为year,但您尝试运行escape(form_data.getfirst('theyear'))getfirst 在没有对应的表单值并且escape 失败一个None 时返回None。出于类似的原因,您需要更好地处理可选字段,例如 Willem 在 cmets 中所说的。

【讨论】:

    【解决方案2】:

    根据错误代码:

    /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 作为参数。根据给定的代码 sn-p,Escape() 在内部直接使用 replace()。因此,快速解决方法是确保您没有将 None 提供给 escape 方法,而可能是一个空字符串。

    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()。

    【讨论】:

    • 这不是 Python 中三元运算符的实现方式,正如 Willem 所建议的那样,or 就足够了。
    • @AlexHall 你是对的。在处理其他代码(java)时输入。相应地更正了示例。矿石就够了,但为了解释问题,我更喜欢更长的代码。
    猜你喜欢
    • 2017-12-28
    • 2017-10-05
    • 2018-03-17
    • 2019-01-10
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多