为什么我的网站在python中抛出POST错误?

问题描述 投票:-3回答:1

我正在向JSON文件的League of Legends API发送请求。在其他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
      }
  }
python json api flask python-requests
1个回答
1
投票

错误并不完全在您认为的地方。

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

错误实际上在上面的第一行(您可以在屏幕截图中看到它)。你调用函数challengerPlayers,但它没有定义(这正是错误消息告诉你的)。

您应该在函数调用中实现此函数或修复名称。

顺便说一句,你调用一个与函数同名的变量,这是一个不好的做法。

© www.soinside.com 2019 - 2024. All rights reserved.