Flask中表单的提交按钮不工作

2024-06-17 09:35:58 发布

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

表单的“提交”按钮不起作用。提交表单后,它应该重定向到下一页,但什么也没有发生。提交时,它应该重定向到localhost:5000/dashboard data链接,然后在网页上打印数据。如果有人能帮我,请。我已经提供了尽可能多的细节

这是dashboard.py

import os
import random
import pandas as pd
from flask import Flask, render_template , request
import sqlite3
import matplotlib.pyplot as plt
import matplotlib
PEOPLE_FOLDER = os.path.join('static')

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = PEOPLE_FOLDER

data = pd.read_excel('C:\\Users\\Desktop\\Projects\\api\\Tweet_qlik_first.xlsx')
from sqlalchemy import create_engine
engine = create_engine('sqlite://', echo=False)

@app.route('/', methods=['GET', 'POST'])
def homepage():
    if request.method=='GET':
        matplotlib.use('Agg')
        data.to_sql('users', con=engine)
        topic_list=engine.execute("SELECT distinct Topic FROM users").fetchall()
        count=engine.execute('''Select Topic,count(*) from users group by Topic''',).fetchall()
        print(count)
        x = []
        y = []
        for tr in count:
            x.append(tr[0])
            y.append(tr[1])
        plt.bar(x,y)

        plt.gcf().subplots_adjust(bottom=0.15)

        plt.xlabel('Topics')
        plt.ylabel('Tweet Count')
        ax = plt.gca()
        plt.setp(ax.get_xticklabels(), rotation=30, horizontalalignment='right')

        plt.tight_layout()
        x=random.randint(0,9999999)
        img_name='plot'+str(x)+'.png'
        plt.savefig('static/'+img_name)

        full_filename = os.path.join(app.config['UPLOAD_FOLDER'], img_name)
        tl=[]
        for tr in topic_list:
            tl.append(tr[0])
        return render_template("main.html",topics=tl,img=full_filename)
@app.route('/dashboard-data',methods=['GET','POST'])
def result():

    if request.method=='POST':
        result=request.form["topic_list"]
        topic_fcount=engine.execute('''Select Topic,"Follower Count" from users where Topic=?''',(str(result),)).fetchall()
        return render_template("dashboard.html")


if __name__ == "__main__":
    app.run(debug=True)

这是main.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action = "/dashboard-data" method = 'POST'>
Topic
<select name="topic_list">

{% for each in topics %}

<option value="{{each}}" selected="{{each}}">{{each}}</option>

{% endfor %}

</select>

<input type="button" value="Submit"/>
</form>
</body>

<img src="{{ img }}" alt="User Image" >
</html>

这是dashboard.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% for row in topic_fcount %}
{{row}}
{%endfor%}
</body>
</html>

Tags: namefromimportappimgdatatopicrequest
2条回答

Submit button of the form is not working. After submitting the form,it should redirect to the next page but nothing is happening.

尝试将按钮更改为:

<input type="submit" value="Submit" />

您将按钮定义为按钮,而不是提交。
您只需要从中编辑按钮

<input type="button" value="Submit"/>

<input type="submit" value="Submit"/>

你会成功的

相关问题 更多 >