pythonanywhere + flask:网站仅显示“未处理异常”。如何让调试器打印堆栈跟踪?
要提醒大家,这里有一个三重新手的挑战 - 新手在使用Python、新手在使用Python Anywhere,还有新手在使用Flask。
[pythonanywhere-root]/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>
当我访问这个网站时,出现了“未处理的异常” :-( 我该怎么做才能让调试器至少告诉我应该从哪里开始找问题呢?
2 个回答
2
你还需要把你在模板中用到的 orgname
变量的名字传递给 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)
3
问题在于 render_template
这个函数只接受一个位置参数,其他的参数只能以关键字的方式传入。所以,你需要把你的代码改成这样:
def buildOrg():
orgname = 'ACME Inc'
return render_template('index.html', name=orgname)
至于第一部分,你可以在 pythonanywhere.com 的 Web
标签下找到错误日志。