pythonanywhere + flask:网站只是说'未处理的异常'。如何让调试器打印堆栈跟踪?

时间:2014-03-26 18:48:55

标签: python flask pythonanywhere

预先警告一个三重新手威胁 - 蟒蛇新手,python anywhere的新手,是烧瓶新手。

[pythonanywhere根] /mysite/test01.py

# A very simple Flask Hello World app for you to get started with...

from flask import Flask
from flask import render_template # for templating
#from flask import request   # for handling requests eg form post, etc

app = Flask(__name__)
app.debug = True #bshark: turn on debugging, hopefully?

@app.route('/')
#def hello_world():
#    return 'Hello from Flask! wheee!!'
def buildOrg():
    orgname = 'ACME Inc'
    return render_template('index.html', orgname)

然后在[pythonanywhere-root] /templates/index.html

<!doctype html>
<head><title>Test01 App</title></head>
<body>
{% if orgname %}
  <h1>Welcome to {{ orgname }} Projects!</h1>
{% else %}
<p>Aw, the orgname wasn't passed in successfully :-(</p>
{% endif %}
</body>
</html>

当我点击网站时,我会得到“未处理的例外”#39; :-( 如何使调试器至少吐出我应该开始寻找问题的位置?

2 个答案:

答案 0 :(得分:3)

问题是render_template只需要一个位置参数,其余参数只作为关键字参数传递。所以,你需要将代码更改为:

def buildOrg():
    orgname = 'ACME Inc'
    return render_template('index.html', name=orgname)

对于第一部分,您可以在pythonanywhere.com上的Web选项卡下找到错误日志。

答案 1 :(得分:1)

您还需要将模板中使用的orgname变量名称传递给render_template

flask.render_template

 flask.render_template(template_name_or_list, **context)

    Renders a template from the template folder with the given context.
    Parameters: 
      template_name_or_list – the name of the template to be rendered, 
      or an iterable with template names the first one existing will be rendered
      context – the variables that should be available in the context of the template.

所以,改变这一行:

return render_template('index.html', orgname)

要:

return render_template('index.html', orgname=orgname)