创建自定义URL并输出HTML表单数据

时间:2016-10-10 10:51:05

标签: python forms python-3.x flask

对于我的第一个Flask项目,我想使用Riot Game的英雄联盟API创建一个基本的Flask应用程序。我已经完成了API的所有处理工作,但是我在输出它时遇到了麻烦。

我从一个页面上的表单中获取输入。

<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 %}

我希望输出页面的网址是动态的&#39; / currentgame / region / username&#39;但在尝试这样做时不断出错。

我的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)

任何有关输出/返回数据的最佳方式的帮助/指示都将受到赞赏。 感谢

1 个答案:

答案 0 :(得分: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>