简体   繁体   English

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

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

I'm sending request to League of Legends API for JSON file.我正在向英雄联盟 API 发送请求以获取 JSON 文件。 In 3 others tries it worked for me, but in last it doesn't.在其他 3 次尝试中它对我有用,但最后它没有。 I have no idea why and can't find any mistakes.我不知道为什么,也找不到任何错误。

My request for getting JSON file我对获取 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()

My website function for return the results.我的网站功能返回结果。 Place, where is mistake, is highlighted with comment.错误的地方用注释突出显示。

@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)

Last, but not least, my website form最后但并非最不重要的是,我的网站表单

<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: Added JSON file 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
      }
  }

The error does not lie exactly where you think it does.错误并不完全在您认为的地方。

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

The error is actually on the first line above (you can see it on your screenshot).错误实际上在上面的第一行(您可以在屏幕截图上看到)。 You call the function challengerPlayers , but it is not defined (that's exactly what the error message is telling you).您调用了函数challengerPlayers ,但它没有定义(这正是错误消息告诉您的)。

You should implement this function or fix the name in the function call.您应该实现此函数或在函数调用中修复名称。

By the way, you call a variable with the same name as a function, which is a bad practice.顺便说一下,你调用一个与函数同名的变量,这是一种不好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM