英文:
my code seem to have trouble when it comes to getting the data from the input textbox to python flask
问题
HTML部分看起来正常,但Python代码中有一些问题。你的代码中使用了request.args.get("ai_chatbox")
来获取用户在文本框中输入的信息,但实际上应该使用request.form.get("ai_chatbox")
,因为你是通过POST方法提交表单数据,而不是通过URL参数传递数据。这应该解决问题。以下是修正后的Python代码:
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/', methods=["GET", "POST"])
def main_login():
if request.method == "POST":
thoughts_ai = request.form.get("ai_chatbox")
return render_template('main_login.html', thoughts_ai=thoughts_ai)
if __name__ == "__main__":
app.run(debug=True)
你还需要在render_template
中传递thoughts_ai
变量,以便在HTML中使用它。这样,用户在文本框中输入的信息将能够在HTML中显示出来。
英文:
my code in python and in html have problems in the direction of the data to go to
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="main_login_design.css">
<title>Projekt Tsarli</title>
<nav class="header">
<div class="header">
<p class="logo">Projekt Tsarli</p>
</div>
</nav>
<br>
<br>
<br>
<br>
<br>
<br>
</head>
<body>
<div class="ai_chatbox">
<div>
<form action="{{url_for(main_login)}}" method="post">
<input name="ai_chatbox" placeholder="Enter your thoughts here" class="chatbox_design">
<button type="submit">Enter</button>
<br>
<br>
</form>
</div>
</div>
</body>
</html>
and in python:
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/', methods=["GET","POST"])
def main_login():
if request.method == "POST":
thoughts_ai = request.args.get("ai_chatbox")
return render_template('main_login.html')
if __name__ == "__main__":
app.run(debug=True)
im trying to connect the info of what the user input in the textbox to python and to return it in html but it doesnt seems to work, ive tried everything.
答案1
得分: 1
You can access the value entered in the text box using request.form.get("ai_chatbox")
. But I think you are not passing this value to the template for rendering.
def main_login():
if request.method == "POST":
thoughts_ai = request.form.get("ai_chatbox")
# Process the form data or perform any required actions
return render_template('index.html', thoughts_ai=thoughts_ai)
return render_template('index.html')
and add these to where you want it. {{thoughts_ai}}
will get what you put in the render_template('index.html', thoughts_ai=thoughts_ai)
.
<div>
<p>User Input: {{ thoughts_ai }}</p>
</div>
英文:
You can access the value entered in the text box using request.form.get("ai_chatbox")
. But I think you are not passing this value to the template for rendering.
def main_login():
if request.method == "POST":
thoughts_ai = request.form.get("ai_chatbox")
# Process the form data or perform any required actions
return render_template('index.html', thoughts_ai=thoughts_ai)
return render_template('index.html')
and add these to where you want it. {{thoughts_ai}}
will get what you put in the render_template('index.html', thoughts_ai=thoughts_ai)
.
<div>
<p>User Input: {{ thoughts_ai }}</p>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论