简体   繁体   English

将来自不同 html 站点的 2 个输入用于一个 app.route?

[英]Use 2 inputs from different html site for one app.route?

Hello I have a question about the Python Flask application.您好,我对 Python Flask 申请有疑问。 I've been looking all the time, but can't find a suitable solution..我一直在寻找,但找不到合适的解决方案..

I would like to search for certain columns from this data set using an input.我想使用输入从该数据集中搜索某些列。 Then I want to multiply these columns with another input foat input and print them on 'calculate.html'然后我想将这些列与另一个输入 foat 输入相乘并将它们打印在'calculate.html'

I have a data set that looks something like this:我有一个看起来像这样的数据集:

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

I have 2 different html pages.我有 2 个不同的 html 页。 The 1st page should pass input 1. The input should be searched in the dataset.第一页应该通过输入 1。应该在数据集中搜索输入。 Nothing should be printed out.什么都不应该打印出来。

index.html:索引.html:

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

On the 2nd HTML page, the number to be multiplied should then be given.在第 2 页 HTML,然后应给出要相乘的数字。 And also it have to print the outputs of this program on this site.而且它还必须在此站点上打印此程序的输出。

calculate.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>

Here is my Python script:这是我的 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')

But I don't understand why it doesn't work.但我不明白为什么它不起作用。 Can't use 2 inputs from 2 different sites in one app.route?不能在一个 app.route 中使用来自 2 个不同站点的 2 个输入?

Your indentation is messed up.你的缩进搞砸了。 My best guess is that it's meant to look like this:我最好的猜测是它看起来像这样:

        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'
            elif 'cm' in column:
                column_edit= column_edit + 'centimeter'

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

Note the for block and subsequent return , along with the else clause, have all been outdented one level.请注意for块和随后的return以及else子句都被缩进了一级。 Previously, the for block would never be reached, as there's a return directly before, it, and the else clause would execute when output != True , as opposed to when request.method != "POST" , which again I assume is what you intended.以前,永远不会到达for块,因为之前直接有一个return ,它和else子句将在output != True时执行,而不是在request.method != "POST"时执行,我再次假设它是什么你打算。

As a comment mentioned, you'd do well to split your code up more to make it easier to follow, and harder to make mistakes like this.正如评论中提到的那样,您最好将代码拆分得更多,以使其更易于遵循,并且更难犯这样的错误。 It doesn't have to be separate routes, but have the GET and POST handling done by different functions, perhaps.它不一定是单独的路由,但可能由不同的函数完成 GET 和 POST 处理。

About how to split the route into 2 separeted routes关于如何将路线分成 2 条分开的路线

index.html index.html

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

(I'm realising now that you do not close your form with the </form> and that the open tag <form> is malformed too) (我现在意识到你没有用</form>关闭你的表单并且打开标签<form>也是格式错误的)

calculate.html计算.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>

view.py查看.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)

Also, as @Adam Barnes said, your indentation is a bit messed up, so I'm not very sure about the part for inpt2另外,正如@Adam Barnes所说,您的缩进有点乱,所以我不太确定inpt2的部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM