pythonanywhere + flask: website just says 'unhandled exception'. How to get debugger to print stack trace?

StackOverflow https://stackoverflow.com/questions/22670403

  •  22-06-2023
  •  | 
  •  

문제

Be forewarned of a triple-newbie threat - new to python, new to python anywhere, new to 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)

And then in [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>

When I hit up the site, I get 'Unhandled Exception' :-( How do I get the debugger to at least spit out where I should start looking for the problem?

도움이 되었습니까?

해결책

The problem is render_template only expects one positional argument, and rest of the arguments are passed as keyword only arguments.So, you need to change your code to:

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

For the first part, you can find the error logs under the Web tab on pythonanywhere.com.

다른 팁

You need to also pass your name of orgname variable that is used in your template to 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.

So, change this line:

return render_template('index.html', orgname)

To:

return render_template('index.html', orgname=orgname)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top