将变量从HTML传递到Python/Flask [英] Passing Variable from HTML to Python/Flask

查看:532
本文介绍了将变量从HTML传递到Python/Flask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我再试一次.我想在HTML表单中输入要提交的变量,到目前为止,这里没有阅读链接

Let me try this again. I want to enter a variable into my HTML form to submit, so far from reading the link here How to display a variable in HTML I've tried the following code, which is inside of main.html

  <form>
      Asset Tag:<br>
      <input type="text" name="Asset Tag"><br>
      <input type="submit" value="Submit">
      <form action="{{ asset_tag }}" method="get">
  </form>

然后我有了一个像这样的python脚本,

I then have a python script that goes like this,

from flask import Flask, render_template
app = Flask('server')

@app.route('/py')
def server():
    return render_template('main.html')
#API URL
JSS_API = 'https://apiresource.com'

#Pre-Defined username and password
username = 'username'
password = 'password'

#Ask User for the Asset tag
asset_tag = {{ }}

输入了资产标签后,它只是在JSON文件中搜索匹配项,其余的无关紧要,因此我不包括脚本的下一部分.

After the asset tag is entered it just searches through a JSON file for match, the rest doesn't matter so much so I didn't include the next piece of the script.

因此Flask可以很好地呈现我的HTML,并且我可以提交一个值,但不会将其传递回脚本,这与我提供的链接相反,这很有意义,但是我无法认为它是如何完成的.有什么建议吗?

So Flask renders my HTML just fine and I can submit a value but it's not being passed back to the script, which makes sense as I'm doing the opposite of the link I provided, but I just can't not think of how it's done. Any suggestions?

推荐答案

您有一些我在下面概述的问题.总体而言,前端 会将变量传递给后端,仅是变量只能通过

You have a few issues that I've outlined below. Overall though, the frontend is passing the variable to the backend, it's just that the variables are only accessible via the request object from within the route to which the data is passed.

  • 我不确定为什么在这里<form>内嵌套了<form>,但是您想要删除内部的内容,因为它没有做任何事情.

  • I am not sure why you have a <form> nested within a <form> here, but you'll want to remove the inner one since it's not doing anything.

您希望设置表单,以便在提交数据时将数据发布到后端.如果您未指定action,则它将在您当前正在查看的同一页面上张贴到同一页面.

You want to setup your form to POST the data to your backend when submitted. If you don't specify an action, then it will POST to the same page the same page that you're currently viewing.

<form method="POST">
    Asset Tag:<br>
    <input type="text" name="tag"><br>
    <input type="submit" value="Submit">
</form>

  • 您需要设置路由以接受POST请求,以便它可以从页面上的表单接收数据.有关HTTP方法的更多信息,请参见此处.

    @app.route('/py', methods=['GET', 'POST'])
    

  • 在您的路由内,您需要检查这是一个GET请求(并加载正常页面)还是一个POST(已发送表单数据,因此我们应该使用它)

  • Inside of your route, you'll want to check whether it was a GET request (and load a normal page) or whether it was a POST (form data was sent so we should use it)

    from flask import request
    
    @app.route('/py', methods=['GET', 'POST'])
    def server():
        if request.method == 'POST':
            # Then get the data from the form
            tag = request.form['tag']
    
            # Get the username/password associated with this tag
            user, password = tag_lookup(tag)
    
            # Generate just a boring response
            return 'The credentials for %s are %s and %s' % (tag, user, password) 
            # Or you could have a custom template for displaying the info
            # return render_template('asset_information.html',
            #                        username=user, 
            #                        password=password)
    
        # Otherwise this was a normal GET request
        else:   
            return render_template('main.html')
    

  • 这篇关于将变量从HTML传递到Python/Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆