My code在从输入文本框获取数据到Python Flask时似乎有问题。

huangapple go评论94阅读模式
英文:

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(&quot;ai_chatbox&quot;). But I think you are not passing this value to the template for rendering.

def main_login():
    if request.method == &quot;POST&quot;:
        thoughts_ai = request.form.get(&quot;ai_chatbox&quot;)

        # Process the form data or perform any required actions

        return render_template(&#39;index.html&#39;, thoughts_ai=thoughts_ai)

    return render_template(&#39;index.html&#39;)

and add these to where you want it. {{thoughts_ai}} will get what you put in the render_template(&#39;index.html&#39;, thoughts_ai=thoughts_ai).

    &lt;div&gt;
        &lt;p&gt;User Input: {{ thoughts_ai }}&lt;/p&gt;
    &lt;/div&gt;

huangapple
  • 本文由 发表于 2023年5月6日 21:53:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定