веб-приложение flask, которое получает два данных от пользователя, принимает данные только в одном поле - PullRequest
0 голосов
/ 20 июня 2020

Я нуб в Flask и Python. Это мой frstfsfs fsf fsfsfs

Моя цель - создать веб-приложение. В моем приложении 3 файла. В браузере есть 3 поля:

The sine2 of    ------------ equals
The cossine2 of ------------ equals
sin2(x) + co2(x)------------ equals

Работает только поле sine2, показывающее значение. Больше ничего не работает.

Цель:

Enter a value in sine2 and click on the equal. The result of sine2 is showed.
The value appears in the field cosine2 and sin2+cos2. After the cliking on equal the cossine2 is showed.

Then click on the square sine to show the value.
The same applies when the cosine value is entered first.

визуализация в браузере

controller.py:

from flask import Flask, render_template, request
from compute import *
from model import InputForm

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST']) 


def index():
    form = InputForm(request.form)

    if request.method == 'POST' and form.validate():
        r = form.r.data
        s = sin2(r)
    else:
        s = None

    return render_template("view.html", form=form, s=s)

    if request.method == 'POST' and form.validate():
        d = form.r.data
        e = cos2(d)
    else:
        e = None

    return render_template("view.html", form=form, e=e)

    if request.method == 'POST' and form.validate():
        f = form.r.data
        h = sin2cos2(f)
    else:
        h = None

    return render_template("view.html", form=form, h=h)

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

compute.py

import math

def sin2(r):
    return (math.sin(r)) ** 2

def cos2(r):
    return (math.cos(r)) ** 2

def sin2cos2(r):
    return (math.sin(r)) ** 2 + (math.cos(r)) ** 2

/ template / view. html

<form method=post action="">
The sine2 of
  {{ (form.r) }}
  <input type=submit value=equals>
{% if s != None %}
{{ s }}
{% endif %}

<div>
The cossine2 of
  {{ (form.r) }}
  <input type=submit value=equals>
{% if e!= None %}
{{ e }}
{% endif %}
</div>

<div>
sin2(x)+cos2(x)
  {{ (form.r) }}
  <input type=submit value=equals>
{% if h!= None %}
{{ h }}
{% endif %}
</div>

</form>

1 Ответ

0 голосов
/ 20 июня 2020

После возврата функция завершается, и любой код, написанный после нее, не запускается. В вашей ситуации у вас есть три оператора возврата, поэтому работает только sine2, потому что все, что происходит после, никогда не запускается. Попробуйте удалить первые два оператора return и заменить последний return на:

return render_template("view.html", form=form, s=s, e=e, h=h)

Надеюсь, это поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...