2017-08-07 48 views
-1

我无法理解运行我的@app.route(“/ buy”)的Python代码时出现的ValueError错误。有人可以解释什么ValueError:查看功能没有返回响应是以及如何避免这种情况?此外,您将如何能够了解发生问题的位置以及给出的错误消息。非常感谢!Python ValueError:查看函数未返回响应

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 

( “/买入”):

@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 %} 
+0

请修正您的注意事项 – Wondercricket

+0

确保您在发布Python代码时准确地重现您的缩进。否则,你会在代码中引入新的错误。 – khelwood

+0

对不起@Wondercricket –

回答

0

ValueError: View function did not return a response意味着你没有返回的URL路径"/buy"什么。

在你的代码中的一个点:

else: 
    break 

您从回路断线,并且该函数返回什么。我会将break替换为return render_template("buy.html")或任何其他响应(如return "Error"abort(500),它会显示内部服务器错误)。希望这对你有所帮助。