【问题标题】:Issues with recieving user input with flask使用烧瓶接收用户输入的问题
【发布时间】:2016-01-18 06:43:17
【问题描述】:

这是我几周前第一次学习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>

【问题讨论】:

  • 请不要竖起不必要的代码墙。请参阅帮助中的this section。

标签: python html python-3.x flask


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    app.run(debug=True)
    

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

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

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

    >>> discriminant
    -31
    

    这是你问题的根源。根据documentation for the math module(加粗强调):

    CPython 实现细节:数学模块主要由围绕平台 C 数学库函数的薄包装器组成。特殊情况下的行为在适当的情况下遵循 C99 标准的附件 F。 当前实现将针对无效操作引发 ValueError,例如 sqrt(-1.0) 或 log(0.0)(其中 C99 附录 F 建议发出无效操作或被零除),并针对溢出的结果引发 OverflowError (例如,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
    

    【讨论】:

    • 该错误与问题中的代码不匹配。视图需要返回类似响应的东西,quadraric 会这样做。
    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多