Python ValueError:View函数未返回响应

时间:2017-08-07 13:42:58

标签: python html python-3.x valueerror

我无法理解我在运行@ app.route的Python代码时遇到的ValueError(" / buy")。有人可以解释 ValueError:View函数没有返回响应是什么以及如何避免这种情况?此外,您还可以通过给出的错误消息了解问题的出现位置。非常感谢!

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1615, in full_dispatch_request
    return self.finalize_request(rv)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1630, in finalize_request
    response = self.make_response(rv)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1725, in make_response
    raise ValueError('View function did not return a response')
ValueError: View function did not return a response

(" /买&#34):

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock."""
if request.method == "POST":
    #get symbol from user
    symbl = request.form.get("symbol")
    if not symbl:
        return apology ("Please insert a symbol")

    while True:
        #number of shares to buy
        num = request.form.get("number")

        try:
            number = float (num)
        except ValueError: 
            return apology ("Please insert valid number of stocks you'd like 
            enter code hereto buy")
            continue
        else:
            break
        if number is None or number == '' or number < 1:
            return apology ("Please insert valid number of stocks you'd like 
            to buy")

        #lookup and save dict in quoted
        quoted = lookup(symbl)
        #if symbl is invalid return apology
        if not quoted:
            return apology ("Not a valid stock")
        else:
            #quotedprice saves price of share
            quotedprice=quoted["price"]
            #price of a single share * the number of shares required to buy
            prc=float(qtd)*number
            #remember session id
            ide = session["user_id"]

            csh=db.execute("SELECT * FROM users WHERE id = :ide", ide=ide)
            #continue if user has enough
            if prc <= csh[0]["cash"]:
                db.execute("INSERT INTO portfolio (id, symbol, price, shares, 
                action, dtime) VALUES (:ide, :symbol, :price, :shares, 'Buy', 
                DateTime('now'))", ide=ide, symbol = symbl, price = prc, 
                shares = number)
                db.execute("UPDATE users SET cash = :cash WHERE id = :ide", 
                cash = csh[0]["cash"] - prc, ide = ide)
                return redirect(url_for("index"))
            else: 
                return apology ("Not enough cash to purchase stocks")
else:
    return render_template("buy.html")

buy.html:

{% extends "layout.html" %}

{% block title %}
    Please enter the Symbol to buy the stock.
{% endblock %}

{% block main %}
    <form action="{{ url_for('buy') }}" method="post">
        <fieldset>
            <div class="form-group">
                <input autocomplete="off" autofocus class="form-control" name="symbol" placeholder="Stock Symbol" type="text"/>
            </div>
             <div class="form-group">
                <input autocomplete="off" autofocus class="form-control" name="number" placeholder="Amount of Stock" type="text"/>
            </div>
            <div class="form-group">
                <button class="btn btn-default" type="submit">Submit</button>
            </div>
        </fieldset>
    </form>
{% endblock %}

1 个答案:

答案 0 :(得分:0)

ValueError: View function did not return a response表示您没有在网址"/buy"返回任何内容。

代码的某一点:

else:
    break

你从循环中断了,函数什么也没有返回。我会将break替换为return render_template("buy.html")或任何其他响应(例如显示内部服务器错误的return "Error"abort(500))。希望这对你有所帮助。