使用flask接收用户输入的问题

时间:2016-01-18 06:43:17

标签: python html python-3.x flask

这是我几周前第一次学习python时编写的一个程序,它简单地解决了二次公式,检查解是否是无关的,并找到了二次图的几个关键特征,包括顶点,对称线,我甚至得到它来考虑激进分子。这一切都很好,花花公子,但只能在控制台中使用。

当我开始将它带到烧瓶应用程序并修改它以接受用户输入时,它只能用于完美而非小数的数字。例如A = 1 B = 4 C = 4。每当输入类似A = 2 b = 1 C = 4的东西时,它就会给出:HTTP 405错误。

main.py:

from flask import Flask, render_template, request
import math

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def quadratic():
    if request.method == 'POST':
        a = float(request.form['a'])
        b = float(request.form['b'])
        c = float(request.form['c'])
        outside = b * -1
        bsquared = b ** 2
        four_a_c = 4 * a * c
        discriminant = bsquared - four_a_c
        bottom = 2 * a
        discriminant_sqrt = math.sqrt(discriminant)
        top = outside + discriminant_sqrt
        top2 = outside - discriminant_sqrt
        root = top/bottom
        root2 = top2/bottom
        equation = a * root ** 2 + b * root + c
        equation2 = a * root2 ** 2 + b * root + c
        if equation < 1 and equation > -1:
            Ex = "Not Extraneous"
        else:
            Ex = "Extraneous"
        if equation2 < 1 and equation2 > -1:
            Ex2 = "Not Extraneous"
        else:
            Ex2 = "Extraneous"

        return render_template('form.html', discriminant=discriminant, a=a, b=b, c=c, outside=outside, bsquared=bsquared, bottom=bottom, root=root, root2=root2, ex=Ex, ex2=Ex2)

    if request.method == 'GET':
        return render_template('form.html')

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

form.html:

<html>
<body>

<form method="POST" action=".">
  A <input id="post_form_id" name="a" value="" />
    B<input id ="post_form_id" name="b" value="" />
    C <input id ="post_form_id" name="c" value="" />
   <input type="submit" />
</form>
    <br />
    {% if a %}
    A: {{ a }} <br />
    B: {{ b }} <br />
    C: {{ c }} <br />
    Roots: <br />

    {{ outside }} + √{{ discriminant }} <br/>
    --------- <br/>
       {{ bottom }}<br/>

    {{ outside }} - √{{ discriminant }} <br/>
    --------- <br/>
       {{ bottom }}<br/>

    Approxomated Roots: <br/>
    {{ root }} <br/>
    {{ ex }}   <br/>
    {{ root2}} <br/>
    {{ ex2 }}  <br/>

    {% endif %}
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您的表单的action属性应为/,而不是.。如果这只是一个单页的Flask应用程序,您甚至可以将action属性完全删除到POST到该表单来自的相同URL。

答案 1 :(得分:0)

当您收到HTTP状态错误代码时,最好的办法是在调试模式下运行您的应用程序。由于您希望在WSGI服务器后面运行您的应用程序,最简单的方法是将您的最后一行更改为

app.run(debug=True)

执行此操作后,您将看到实际错误。在这种情况下,它是

# snip
    discriminant_sqrt = math.sqrt(discriminant)
ValueError: math domain error

使用交互式调试器,我们可以检查原因。

>>> discriminant
-31

这是你问题的根源。根据{{​​3}}(为强调增加了粗体):

  

CPython实现细节:math模块主要包含围绕平台C数学库函数的瘦包装器。特殊情况下的行为在适当情况下遵循C99标准的附录F. 当前实现将引发无效操作的ValueError,如sqrt(-1.0)或log(0.0)(其中C99附件F建议发出无效操作信号或被零除),溢出结果溢出错误(例如,exp(1000.0))。

math.sqrt(a_negative_number)会提出ValueError。您给出的输入没有解决方案(或者有一个复杂的解决方案,具体取决于您如何看待它)。您可以做的最好的事情是检查这种情况并向用户提供更好的反馈。

try:
    discriminant_sqrt = math.sqrt(discriminant)
except ValueError:
    flash('There is no solution for the given inputs.', 'error')
else
    # continue with calculations
相关问题