3

预先警告三重新手威胁 - python 新手,任何地方的 python 新手的 python 新手,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>

当我访问该站点时,我得到“未处理的异常”:-(我如何让调试器至少吐出我应该从哪里开始寻找问题?

4

2 回答 2

3

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

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

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

于 2014-03-26T19:04:34.350 回答
2

您还需要将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)
于 2014-03-26T19:04:21.870 回答