2016-10-10 41 views
0

对于我的第一个Flask项目,我想创建一个基本的Flask应用程序,使用Riot Game的API for League of Legends。我已经完成了API工作的所有处理,但是我无法输出它。创建一个自定义URL并输出HTML表单数据

我从一页上的表单中接受输入。

<form class="navbar-form navbar-left" action="{{ url_for('current_game_output') }}" method="POST"> 
    <div class="form-group"> 
     <input type="text" class="form-control" placeholder="Summoner Name" name="summoner_name"> 
     <select class="form-control" name="region"> 
      <option value="oce">Oceanic</option> 
      <option value="na">North America</option> 
      <option value="euw">Europe West</option> 
     </select> 
    </div> 
    <button type="submit" class="btn btn-default" value="Send">Submit</button> 
</form> 

而我试图将从API返回的数据输出到下一页。

{% extends "header.html" %} 
{% block body %} 

    <h3> Team 2 </h3> 
    <table class="table table-bordered" width="50%"> 
     <tr> 
      <th width="48%">Summoner Name</th> 
      <th width="48%">Champion</th> 
      <th width="4%">Pic</th> 
     </tr> 
     {% for player in team1 %} 
      <tr> 
       <td>{{ player[0] }}</td> 
       <td>{{ player[1] }}</td> 
       <td><img width="20px" src="{{ url_for('static', filename='images/championIcons/') }}{{ player[1].replace(" ", "") }}_Square_0.png"></td> 
      </tr> 
     {% endfor %} 
    </table> 

    <h3> Team 1 </h3> 
    <table class="table table-bordered" width="50%"> 
     <tr> 
      <th width="48%">Summoner Name</th> 
      <th width="48%">Champion</th> 
      <th width="4%">Pic</th> 
     </tr> 
     {% for player in team2 %} 
      <tr> 
       <td>{{ player[0] }}</td> 
       <td>{{ player[1] }}</td> 
       <td><img width="20px" src="{{ url_for('static', filename='images/championIcons/') }}{{ player[1].replace(" ", "") }}_Square_0.png"></td> 
      </tr> 
     {% endfor %} 
    </table> 
{% endblock %} 

我想输出页面是动态的,“/ currentgame /区域/用户名”的网址,但继续尝试这样做时收到错误。我views.py文件

相关部分(隐藏我的API密钥):

@app.route('/header') 
def header(): 
    return render_template("header.html") 

@app.route('/current') 
def current(): 
    return render_template("current.html") 


@app.route('/currentgame/<region>/<name>', methods=['POST']) 
def current_game_output(region, name): 
    region = request.form['region'] 
    summoner_name = request.form['summoner_name'] 
    api = RiotAPI('APIKEYGOESHERE', region) 
    team1, team2 = current_game_data(summoner_name, region, api) 
    return render_template("output.html",team1=team1,team2=team2) 

任何帮助/上输出的最佳方法指针/返回数据,将不胜感激。 谢谢

回答

0

你也应该发布错误。

在快速看起来这应该是固定的:

@app.route('/currentgame/<string:region>/<string:name>', methods=['POST']) 
def current_game_output(region, name): 
    region = request.form['region'] 
    summoner_name = request.form['summoner_name'] 
    api = RiotAPI('APIKEYGOESHERE', region) 
    team1, team2 = current_game_data(summoner_name, region, api) 
    return render_template("output.html",team1=team1,team2=team2) 

更改路线/currentgame/<string:region>/<string:name>

+0

好了,谢谢。我试过修复你的建议,并收到“内部服务器错误”。我查看了日志,并发现了这些错误:http://pastebin.com/1d1zGP9W –

+0

@AConway更新答案。现在看看。 –

+0

谢谢,那解决了那个错误。不幸的是,我现在收到这个错误http://pastebin.com/hei1S4B0。有没有我想要设置URL的变量的地方?谢谢 –

相关问题