首页 > 解决方案 > 为一个 app.route 使用来自不同 html 站点的 2 个输入?

问题描述

您好,我有一个关于 Python Flask 应用程序的问题。我一直在寻找,但找不到合适的解决方案..

我想使用输入从该数据集中搜索某些列。然后我想将这些列与另一个输入 foat 输入相乘并将它们打印在“calculate.html”上

我有一个看起来像这样的数据集:

df=    
    A           B(m)         C(cm)      D(m)
    house        50           50        100
    bedroom      80           50        600
    bed          20           60        500 

我有 2 个不同的 html 页面。第 1 页应通过输入 1。应在数据集中搜索输入。不应打印任何内容。

索引.html:

<form> action ='/index' method = 'post'>
    <p>< input type = 'text' name='inpt'/> </p>
    <p>< input type = 'submit' name='submit'/> </p>

在第二个 HTML 页面上,应给出要相乘的数字。而且它还必须在这个网站上打印这个程序的输出。

计算.html

<h1>{{inpt}}</h1>
<form> action ='/index' method = 'post'>
    <p>< input type = 'text' name='inpt2'/> </p>
    <p>< input type = 'submit' name='submit'/> </p>
    
    <h3>{{my_column}}</h3>    
    <h3>{{my_numbers}}</h3>

这是我的 Python 脚本:

@app.route('/index', methods=['POST', 'GET'])
def index():
    my_numbers = []
    my_column = []
    if request.method == 'POST'
        inpt = request.form['inpt']
        inpt2 = request.form['inpt2']
        output = False
  
    
        for text in df['A']:
            if text == inpt:
                give_text = df[df['A'] == inpt]
                give_text = give_text.drop(['A'], axis = 1)
                output = True
        
        if output == True:
           return render_template('calculate.html', inpt = inpt)
           # At this point the program should redirect to 'calculate.html' in order to continue calculating there

           # Here we start to calculate with inpt2
            for column in give_text:
                column_edit = give_text[column]
                my_columns.append(column)
                column_edit = float(column_edit) * float(inpt2)
            
                if '(m)' in column:
                    column_edit = column_edit + 'meter'
                    my_numbers.append(column_edit)
                elif 'cm' in column:
                    column_edit= column_edit + 'centimeter'
                    my_numbers.append(column_edit)

            return render_template('calculate.html', my_numbers = my_numbers, my_column = my_column)
        else:
            return render_template('index.html', content = 'No result')

但我不明白为什么它不起作用。不能在一个 app.route 中使用来自 2 个不同站点的 2 个输入?

标签: pythonflask

解决方案


关于如何将路线分成 2 条分开的路线

索引.html

<form action ='/index' method='post'>
    <p><input type='text' name='inpt'/> </p>
    <p><input type='submit' name='submit'/> </p>
</form>

(我现在意识到你没有关闭你的表单,</form>并且打开的标签<form>也是格式错误的)

计算.html

<h1>{{inpt}}</h1>
<form action='/calculate' method='post'>
    <input type='text' value='{{inpt}}' style='display:none;'>
    <p>< input type = 'text' name='inpt'/> </p>
    <p>< input type = 'submit' name='submit'/> </p>
    
    <h3>{{my_column}}</h3>    
    <h3>{{my_numbers}}</h3>
</form>

视图.py

@app.route('/index', methods=['GET'])
def index():
    return render_template('index.html')


@app.route('/index', methods=['POST'])
def index_post():
    my_numbers = []
    my_column = []

    inpt = request.form['inpt']
    output = False
      
    for text in df['A']:
        if text == inpt:
            give_text = df[df['A'] == inpt]
            give_text = give_text.drop(['A'], axis = 1)
            output = True
        
    if output == True:
       return render_template('calculate.html', inpt=inpt)
    else:
       return render_template('index.html', content='No result')


@app.route('/calculate', methods=['POST'])
def calculate():
    my_numbers = []
    my_column = []

    inpt = request.form['inpt']
    inpt2 = request.form['inpt2']
    output = False
     
    for text in df['A']:
        if text == inpt:
            give_text = df[df['A'] == inpt]
            give_text = give_text.drop(['A'], axis = 1)

    # Here we start to calculate with inpt2
    for column in give_text:
        column_edit = give_text[column]
        my_columns.append(column)
        column_edit = float(column_edit) * float(inpt2)
        
        if '(m)' in column:
            column_edit = column_edit + 'meter'
            my_numbers.append(column_edit)
        elif 'cm' in column:
            column_edit= column_edit + 'centimeter'
            my_numbers.append(column_edit)

    return render_template('calculate.html', my_numbers=my_numbers, my_column=my_column, inpt=inpt)

另外,正如@Adam Barnes所说,您的缩进有点混乱,所以我不太确定inpt2


推荐阅读