# hello.py
from datetime import datetime
from flask import Flask, render_template
from flask_script import Manager
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required
class NameForm(Form):
name = StringField('What is your name?', validators=[Required])
submit = SubmitField('Submit')
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
@app.route('/')
def index():
return render_template('index.html', current_time=datetime.utcnow())
@app.route('/user/')
def user(name):
return render_template('user.html', name=name)
if __name__ == '__main__':
manager.run()
{% extends "base.html" %}
{% block title %}Flasky - Internal Server Error{% endblock %}
{% block page_content %}
Internal Server Error
{% endlock %}
报错信息:
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
[2017-07-13 14:17:47,740] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/henry/.local/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "hello.py", line 32, in index
return render_template('index.html', current_time=datetime.utcnow())
File "/home/henry/.local/lib/python3.5/site-packages/flask/templating.py", line 134, in render_template
context, ctx.app)
File "/home/henry/.local/lib/python3.5/site-packages/flask/templating.py", line 116, in _render
rv = template.render(context)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/home/henry/dev/Flask_Web_Python_Henry/flask_henry/templates/index.html", line 2, in top-level template code
{% import "bootstrap/wtf.html" as wtf %}
File "/home/henry/dev/Flask_Web_Python_Henry/flask_henry/templates/base.html", line 1, in top-level template code
{% extends "bootstrap/base.html" %}
File "/home/henry/.local/lib/python3.5/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 1, in top-level template code
{% block doc -%}
File "/home/henry/.local/lib/python3.5/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 4, in block "doc"
{%- block html %}
File "/home/henry/.local/lib/python3.5/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 20, in block "html"
{% block body -%}
File "/home/henry/.local/lib/python3.5/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 23, in block "body"
{% block content -%}
File "/home/henry/dev/Flask_Web_Python_Henry/flask_henry/templates/base.html", line 35, in block "content"
{% block page_content %}{% endblock %}
File "/home/henry/dev/Flask_Web_Python_Henry/flask_henry/templates/index.html", line 10, in block "page_content"
{{ wtf.quick_form(form) }}
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/runtime.py", line 553, in _invoke
rv = self._func(*arguments)
File "/home/henry/.local/lib/python3.5/site-packages/flask_bootstrap/templates/bootstrap/wtf.html", line 200, in template
{{ form.hidden_tag() }}
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'form' is undefined
127.0.0.1 - - [13/Jul/2017 14:17:47] "GET / HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
File "/home/henry/.local/lib/python3.5/site-packages/werkzeug/serving.py", line 209, in run_wsgi
execute(self.server.app)
File "/home/henry/.local/lib/python3.5/site-packages/werkzeug/serving.py", line 197, in execute
application_iter = app(environ, start_response)
File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/henry/.local/lib/python3.5/site-packages/flask/app.py", line 1547, in handle_exception
return self.finalize_request(handler(e), from_error_handler=True)
File "/home/henry/dev/Flask_Web_Python_Henry/flask_henry/hello.py", line 28, in internal_server_error
return render_template('500.html'), 500
File "/home/henry/.local/lib/python3.5/site-packages/flask/templating.py", line 133, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/loaders.py", line 125, in load
code = environment.compile(source, name, filename)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/environment.py", line 591, in compile
self.handle_exception(exc_info, source_hint=source_hint)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/home/henry/dev/Flask_Web_Python_Henry/flask_henry/templates/500.html", line 9, in template
{% endlock %}
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/environment.py", line 497, in _parse
return Parser(self, source, name, encode_filename(filename)).parse()
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/parser.py", line 896, in parse
result = nodes.Template(self.subparse(), lineno=1)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/parser.py", line 878, in subparse
rv = self.parse_statement()
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/parser.py", line 130, in parse_statement
return getattr(self, 'parse_' + self.stream.current.value)()
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/parser.py", line 268, in parse_block
node.body = self.parse_statements(('name:endblock',), drop_needle=True)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/parser.py", line 165, in parse_statements
result = self.subparse(end_tokens)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/parser.py", line 878, in subparse
rv = self.parse_statement()
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/parser.py", line 144, in parse_statement
self.fail_unknown_tag(token.value, token.lineno)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/parser.py", line 97, in fail_unknown_tag
return self._fail_ut_eof(name, self._end_token_stack, lineno)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/parser.py", line 90, in _fail_ut_eof
self.fail(' '.join(message), lineno)
File "/home/henry/.local/lib/python3.5/site-packages/jinja2/parser.py", line 59, in fail
raise exc(msg, lineno, self.name, self.filename)
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endlock'. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'.