从HTML页面获取输入,并将输入传递到另一个Python文件中的函数中

2024-05-17 03:20:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从html文件中获取用户输入,并将其传递到位于同一目录下另一个python文件中的函数中

用户应该在html网页中输入用户名和密码,这些输入将被传递到另一个python文件中,以运行许多验证函数

非常感谢您的帮助或指导:)

多谢各位

form.html文件

<form action="{{ url_for("gfg")}}" method="post">
<label for="username">username:</label>
<input type="text" id="username" name="username" placeholder="username">
<label for="password">password:</label>
<input type="text" id="password" name="password" placeholder="password">
<button type="submit">Login</button>

app.py文件

# importing Flask and other modules
from flask import Flask, request, render_template

# Flask constructor
app = Flask(__name__)



# A decorator used to tell the application
# which URL is associated function
@app.route('/', methods=["GET", "POST"])
def gfg():
   if request.method == "POST":
      # getting input with name = fname in HTML form
      username = request.form.get("username")
      # getting input with name = lname in HTML form
      password = request.form.get("password")

      return username + password
   return render_template("form.html")

if __name__ == '__main__':
   app.run()

主python文件(函数所在的位置)

def main():
    
    username = app.gfg()[0]
    password = app.gfg()[1]
    TestLogin(username, password)

if __name__ == "__main__":
    main()

error message


Tags: 文件函数nameformappflaskforinput
1条回答
网友
1楼 · 发布于 2024-05-17 03:20:36

您需要使用请求上下文

RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed an active HTTP request. Consult the documentation on testing for information about how to avoid this problem.

[...]
with app.test_request_context(
        '/url/', data={'format': 'short'}):
    username = app.gfg()[0]
    password = app.gfg()[1]
    TestLogin(username, password)
    [...] 

你可以看看docs

相关问题 更多 >