簡體   English   中英

為什么我的網站在 python 中拋出 POST 錯誤?

[英]Why is my website throwing POST error in python?

我正在向英雄聯盟 API 發送請求以獲取 JSON 文件。 在其他 3 次嘗試中它對我有用,但最后它沒有。 我不知道為什么,也找不到任何錯誤。

我對獲取 JSON 文件的請求

def challengerPlayers(region, types, APIkey):
    URL = "https://" + region + ".api.pvp.net/api/lol/" + region + "/v2.5/league/master?type=" + types + "&api_key=" + APIkey
    response = requests.get(URL)
    return response.json()

我的網站功能返回結果。 錯誤的地方用注釋突出顯示。

@app.route('/hello', methods=['post'])
def hello():
    region = request.form['region']
    summonerName = request.form['summonerName']
    APIkey = request.form['APIkey']
    types = request.form['types']
    responseJSON = getData(region, summonerName, APIkey)
    ID = responseJSON[summonerName]['id']
    ID = str(ID)
    responseJSON2 = getRankedData(region, ID, APIkey)
    divisionName = responseJSON2[ID][0]['name']
    responseJSON3 = challengerPlayers(region, str(types), APIkey)
    #Here is the problem ↓↓↓
    challengerPlayers = responseJSON3['entries'][0]
    #print challengerPlayers    
    return render_template('form_action.html', ID = ID,  divisionName = divisionName, challengerPlayers = challengerPlayers)

最后但並非最不重要的是,我的網站表單

<form class="form" method="post" action="/hello">
        <div class="form-group">
            <label for="regio">Region</label>
            <input type="text" name="region" />
        </div>
        <div class="form-group">
            <label for="summonerNam">Summoner Name</label>
            <input type="text" name="summonerName" />
        </div>
        <div class="form-group">
            <label for="apiKe">API Key</label>
            <input type="text" name="APIkey" />
        </div>
        <div class="form-group">
            <label for="type">Ranked type</label>
            <input type="text" name="types" />
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
    </form>

PS:添加了JSON文件

{
   "queue": "RANKED_SOLO_5x5",
   "name": "Nasus's Agents",
   "entries": [
      {
         "leaguePoints": 0,
         "isFreshBlood": false,
         "isHotStreak": false,
         "division": "I",
         "isInactive": false,
         "isVeteran": true,
         "losses": 402,
         "playerOrTeamName": "Ä  L  F  A",
         "playerOrTeamId": "28880245",
         "wins": 445
      }
  }

錯誤並不完全在您認為的地方。

responseJSON3 = challengerPlayers(region, str(types), APIkey)
#Here is the problem ↓↓↓
challengerPlayers = responseJSON3['entries'][0]
#print challengerPlayers    

錯誤實際上在上面的第一行(您可以在屏幕截圖上看到)。 您調用了函數challengerPlayers ,但它沒有定義(這正是錯誤消息告訴您的)。

您應該實現此函數或在函數調用中修復名稱。

順便說一下,你調用一個與函數同名的變量,這是一種不好的做法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM