【问题标题】:Create a custom URL and outputting HTML form data创建自定义 URL 并输出 HTML 表单数据
【发布时间】:2016-10-10 10:51:05
【问题描述】:

对于我的第一个 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 %}

我希望输出页面的 URL 是动态的“/currentgame/region/username”,但在尝试这样做时不断出错。

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)

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

【问题讨论】:

    标签: python forms python-3.x flask


    【解决方案1】:

    您也应该发布错误。

    快速浏览一下,这应该是固定的:

    @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/&lt;string:region&gt;/&lt;string:name&gt;

    【讨论】:

    • 好的,谢谢。我尝试了您建议的修复并收到“内部服务器错误”。我查看了日志,它有这些错误:pastebin.com/1d1zGP9W
    • @AConway 更新答案。现在就来看看吧。
    • 谢谢,解决了这个错误。不幸的是,我现在收到此错误 pastebin.com/hei1S4B0 。有没有我打算为 URL 设置变量的地方?谢谢
    • 这个错误是因为你没有在表单的url_for 中指定regionname 参数。 url_for('current_game_output' , region = some_val , name = some_other_val )
    • 谢谢,解决了这个错误,但现在我收到了 404 not found 错误。
    猜你喜欢
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2020-11-11
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    相关资源
    最近更新 更多