简体   繁体   中英

Return data to same template with python/flask

I'm trying to return value from my python code to template, but nothing changes in html output.

Does somebody know what is wrong?

Flask Code:

    @app.route('/')
def index():
    
    return render_template('index.html', result = 0)


@app.route('/calculadora', methods=['GET', 'POST'])
def calculadora():
    
    
    if request.method == 'POST':
        
        data = request.form.get("data")

        #do something with data
        #result = recebe_dados(data)
        #print(result)
        
        result = data
        
        
        return render_template('index.html', result = result)
        
    else:
        return render_template('index.html')

HTML code:

<body>
        <div class="container">
            <div class="row">
                <div class="col s12 l4 offset-l4 xl4 offset-xl4">
                    <div class="card-panel  indigo lighten-5 right-align screen">
                        <span class="black-text result">{{ result }}</span>
                    
                
<body>
Result:
{{result}}
<form action="/calculadora" method="post">
    <input type="text" name="data" id="">
    <input type="submit" value="submit">
</form>
</body>

Make sure that the your index.html contains the form with the correct route and input tag with the same name as you have used in the app.py file to get the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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