2017-03-24 20 views
0

私は関数の外でコードを実行し、returnの代わりにprintを使用すると問題がないようですので、私は混乱します。はimmutableMultiDictでなくてはなりません - Python/Flask

私は次のようにフォームを経由してフラスコにHTMLからデータを送信しています:

<form method="POST" action="/"> 
    <h4>Search for Your Device</h4> 
    <p>Enter the Asset Tag of the device - On the back sticker or in small print at the bottom of the lock screen.</p> 
    <p><input type = "text" name = "Name" /></p> 
    <p><input type = "submit" value = "submit" /></p> 
</form> 

私は、その入力を取ると、以下の機能

@app.route('/', methods=["GET","POST"]) 
def homepage(): 
    try: 
     if request.method == "POST": 
      #API URL 
      JSS_API = 'https://private_url.com' 
      #Pre-Defined username and password 
      username = 'username' 
      password = 'password' 

      #Ask User for the Asset tag 
      asset_tag = request.form 
      New_JSS_API = JSS_API + asset_tag 

      #Disables Warnings about SSL 
      requests.packages.urllib3.disable_warnings() 

      JSS_Asset_Response = requests.get(New_JSS_API, auth=(username, password), verify=False, headers={'Accept': 'application/json'}) 

      JSS_json = JSS_Asset_Response.json() 

      email_dict = {} 
      for item in JSS_json['mobile_devices']: 
       email_dict["Stu_name".format(item)]=item['realname'] 
      #Can call the dictionary value by doing the following: 
      stu_name = email_dict['Stu_name'] 
      return stu_name 

     return render_template("index.html") 
    except Exception as e: 
     return(str(e)) 
と私のモバイルデバイス管理サーバへのAPIリクエストを作ります

私が受け取るエラーは、 "immutableMultiDictではなくstrでなければなりません"ですが、return stu_nameを端末に交換するとprint(stu_name)と交換されました。

私の目標は、フォームから入力し、生徒の名前をWebページに戻すことです。私はrequest.form仮定

+0

完全トレースを共有する – Dan

+0

@Danエラーは端末で 'app.run(debug = True) 'を指定してもブラウザに返されます。私のPOSTが成功した200の応答しか受信しませんが、pythonエラーは起こりません。 – BrettJ

答えて

2

は、あなたがエラーを得た理由です、あなたは文字列へImmutableMultiDictを追加することはできませんのでJSS_APIは、文字列であり、ImmutableMultiDictです。

request.form.to_dict() 

をそしてデバッグ、コードは、フォームデータを取得し、私は、JSON文字列を取得するためにrequest.form.to_dict().values()[0]を使用するために使用される:

あなたはImmutableMultiDictは、のdictに変換することができます。

それとも

あなたは、このようなパラメータを取得することができ POST方法を使用している場合:

username = request.form.getlist('username[]') 

GETメソッドを、これを使用する:

username = request.args.getlist('username[]') 

docから詳細をご覧ください。

+0

ありがとうございます。私は私がやっていたように私が付けることができなかったことを知らなかった。既にそれを修正した:) – BrettJ

関連する問題