2017-03-29 15 views
-2

enter image description hereはAttributeError:「NoneType」オブジェクトが属性を持っていない(パイソン)「を置換」

http://cs1.ucc.ie/~adc2/cgi-bin/lab7/index.html あなたは、ちょうど箱の誰に何を入力することにより、自分のためのエラーをチェックアウトすることができ、すべてである必要はありdoesntの、任意のヘルプは 素晴らしいことだ、私はこの

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

エラーについてはっきりしないもの: 'replace'を呼び出して、おそらく' str'であると仮定していますが、 'None'です。だから私は入力が正しくAPIを介して実行されないと思う。 –

+0

ありがとう、ありがとう、iveがコードを追加しました –

+0

そのコードで 'str.replace()'の呼び出しが表示されません。あなたの質問に完全なトレースバックを提供してください。 – cdarke

答えて

0

あなた<input>後のコードに送信されますyearという名前が、あなたはescape(form_data.getfirst('theyear'))を実行しようとしています。 getfirstは対応するフォーム値がなく、escapeが1つ失敗した場合Noneを返します。None。同様の理由から、Willem氏がコメントで述べたようなオプションのフィールドをうまく処理する必要があります。エラーコードによると

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 

エスケープは()の引数として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では廃止され、代わりに)(html.escapeを使用しています。

+0

これは、三項演算子がPythonでどのように行われているかではなく、ウィレムが示唆するように 'または'でも十分です。 –

+0

@AlexHallあなたは正しいです。他のコード(java)で作業中に型付けされました。それに応じて例を修正しました。鉱石で十分ですが、問題を説明するために、私はより長いコードを好む。 – ingofreyer

関連する問題