如何从输入中获取表单数据作为 Flask 中的变量? [英] How to get form data from input as variable in Flask?

查看:35
本文介绍了如何从输入中获取表单数据作为 Flask 中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的用户界面,用于通过 ID 启动和停止游戏.我写的基本HTML如下(game_id由JS填充):

 

<div align="left" class="game-id-input">游戏ID:

<div align="right" class="buttons"><form action="{{ url_for('start_game', game_id=game_id) }}" method="get"><input type="submit" name="start" value="Start game" class="btn btn-success"></input></表单><form action="{{ url_for('end_game', game_id=game_id) }}" method="get"><input type="submit" name="end" value="End game" class="btn btn-danger"></input></表单>

看起来像

我还为每个表单定义了 Flask 路由函数:

@app.route("/start_game/")def start_game(game_id):# ...@app.route("/end_game/")def end_game(game_id):# ...

在我的表单中,如何让 game_id 对应于 #game_id 中的 game_id?

目前,当我提交开始和结束游戏时,我收到文件未找到错误,因为它只是将文字 <game_id> 附加到路线.

我是 Web 开发的新手.这应该是微不足道的,但我不知道要搜索什么.这么简单的问题,提前抱歉.

解决方案

您正在尝试根据用户输入生成 url,但是 Jinja 在服务器端渲染模板时用户输入不可用,它仅可用在客户端.因此,如果您想将游戏 ID 作为 URL 参数发布到 URL,则必须在客户端使用 JavaScript 构建该 URL.

对于您正在尝试做的事情,这并不是真正必要的.您可以使用 request.form['name'] 获取命名输入的提交值.按钮就像任何其他输入一样,因此您可以命名它们以了解执行的操作.

@app.route('/manage_game', methods=['POST'])def manage_game():start = request.form['action'] == 'Start'game_id = request.form['game_id']如果开始:start_game(game_id)别的:stop_game(game_id)返回重定向(url_for('index'))

<输入类型=提交"名称=动作"值=开始"/><输入类型=提交"名称=动作"值=停止"/></表单>

即使这样也比您需要的更冗长.鉴于您知道游戏是否已经在进行中,只需切换当前状态而不是选择操作.开始一个已经开始的游戏是没有意义的,只有停止它.

I'm working on a simple UI to start and stop games by ID. The basic HTML I have written is as follows (game_id is populated by JS):

  <div align="center" class="top">
    <div align="left" class="game-id-input">
      Game ID: <input type="text" name="game_id" id="game_id">
    </div>

    <div align="right" class="buttons">
      <form action="{{ url_for('start_game', game_id=game_id) }}" method="get">
        <input type="submit" name="start" value="Start game" class="btn btn-success"></input>
      </form>
      <form action="{{ url_for('end_game', game_id=game_id) }}" method="get">
        <input type="submit" name="end" value="End game" class="btn btn-danger"></input>
      </form>
    </div>
  </div>

which looks like

I also have Flask route functions defined for each of the forms:

@app.route("/start_game/<game_id>")
def start_game(game_id):
    # ...

@app.route("/end_game/<game_id>")
def end_game(game_id):
    # ...

In my forms, how can I make game_id correspond to the game_id from #game_id?

Currently when I submit start and end games, I get a File Not Found error because it's just appending the literal <game_id> to the route.

I'm new to web development. This should be trivial, but I don't know what to search for. Sorry in advance for such a simple question.

解决方案

You are trying to generate a url based on user input, but user input isn't available when Jinja is rendering the template on the server side, it's only available on the client side. So if you wanted to post to URLs with the game id as a URL parameter, you would have to build that URL on the client side with JavaScript.

For what you're trying to do, that's not really necessary. You can get the submitted value of a named input with request.form['name']. Buttons are just like any other input, so you can name them to find out what action was taken.

@app.route('/manage_game', methods=['POST'])
def manage_game():
    start = request.form['action'] == 'Start'
    game_id = request.form['game_id']
    
    if start:
        start_game(game_id)
    else:
        stop_game(game_id)

    return redirect(url_for('index'))

<form method="POST" action="{{ url_for('manage_game') }}">
    <input type="text" name="game_id"/>
    <input type="submit" name="action" value="Start"/>
    <input type="submit" name="action" value="Stop"/>
</form>

Even that's more verbose than you need. Given that you'd know if a game was already in progress, just toggle the current status instead of picking an action. It would never make sense to start a game that's already started, only stop it.

这篇关于如何从输入中获取表单数据作为 Flask 中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆