3

トリプル初心者の脅威について事前に警告してください - Python の初心者、Python どこでも初めて、フラスコの初心者。

[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、1 つの位置引数のみを想定しており、残りの引数はキーワードのみの引数として渡されるため、コードを次のように変更する必要があります。

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(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 に答える