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

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

问题描述

我正在制作一个简单的用户界面来启动和停止ID的游戏。我写的基本HTML如下( game_id 由JS填充):

 < div align =centerclass =top> 
< div align =leftclass =game-id-input>
游戏ID:< input type =textname =game_idid =game_id>
< / div>

< div align =rightclass =buttons>
< input type =submitname =startvalue =开始游戏class =btn btn-success>< / input>
< / form>
< input type =submitname =endvalue =End gameclass =btn btn-danger>< / input>
< / form>
< / div>
< / div>

看起来像


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

$ $ $
def start_game(game_id):
#...

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

在我的表单中,我怎样才能让 game_id 对应 game_id #game_id



目前,当我提交开始和结束游戏时,出现File Not Found错误,只需将< game_id> 文字追加到路线中即可。 我是新开发的网页。这应该是微不足道的,但我不知道要搜索什么。提前对不起,这样一个简单的问题。

解决方案

您正试图生成一个基于用户输入的网址,但用户输入isn当Jinja在服务器端渲染模板时可用,它只在客户端可用。因此,如果您想以网址参数的形式发布到具有游戏ID的网址,则必须使用JavaScript在客户端上构建该网址。



再试图做,这不是真的有必要。你可以通过 request.form ['name'] 来获得一个命名输入的提交值。按钮就像任何其他的输入,所以你可以命名他们来找出采取了什么行动。

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

return redirect(url_for ('index'))



 < form method =POST> 
< input type =textname =game_id/>
< input type =submitname =actionvalue =Start/>
< input type =submitname =actionvalue =Stop/>
< / form>

即使这样比您需要的更冗长。考虑到你会知道游戏是否已经在进行,只需切换当前状态而不是选择一个动作。开始已经开始的游戏永远不会有意义,只能停止它。


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">
    <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天全站免登陆