我正在尝试使用Flask创建一个示例应用程序。一切都很好,但问题从无处发生。
我的追溯和源文件附在此帖子上。我想提一下,我已经尝试改变行结束格式(类UNIX,类似Windows)和编码(ANSI,UTF-8,UTF-8无BOM)。
Traceback (most recent call last):
File "C:\Python27\MyProject\venv\Lib\site-packages\flask\app.py", line 1836, in __call__
File "C:\Python27\MyProject\venv\Lib\site-packages\flask\app.py", line 1820, in wsgi_app
File "C:\Python27\MyProject\venv\Lib\site-packages\flask\app.py", line 1403, in handle_exception
File "C:\Python27\MyProject\venv\Lib\site-packages\flask\app.py", line 1817, in wsgi_app
File "C:\Python27\MyProject\venv\Lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
File "C:\Python27\MyProject\venv\Lib\site-packages\flask\app.py", line 1381, in handle_user_exception
File "C:\Python27\MyProject\venv\Lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
File "C:\Python27\MyProject\venv\Lib\site-packages\flask\app.py", line 1461, in dispatch_request
File "c:\Python27\MyProject\runserver.py", line 22, in index
return render_template('index.html')
File "C:\Python27\MyProject\venv\Lib\site-packages\flask\templating.py", line 127, in render_template
File "C:\Python27\MyProject\venv\Lib\site-packages\jinja2\environment.py", line 830, in get_or_select_template
File "C:\Python27\MyProject\venv\Lib\site-packages\jinja2\environment.py", line 791, in get_template
File "C:\Python27\MyProject\venv\Lib\site-packages\jinja2\environment.py", line 765, in _load_template
File "C:\Python27\MyProject\venv\Lib\site-packages\jinja2\loaders.py", line 125, in load
File "C:\Python27\MyProject\venv\Lib\site-packages\jinja2\environment.py", line 554, in compile
File "C:\Python27\MyProject\venv\Lib\site-packages\jinja2\environment.py", line 742, in handle_exception
File "c:\Python27\MyProject\templates\index.html", line 1, in template
{% extends "base.html" %}
TemplateSyntaxError: unicode-escape
这是我的runserver.py:
# -*- coding: utf-8 -*-
import os
from flask import Flask, render_template, send_from_directory, url_for
# initialization
app = Flask(__name__)
app.config.update(
DEBUG = True,
)
# controllers
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'ico/favicon.ico')
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.route("/")
def index():
return render_template('index.html')
@app.route("/base")
def base():
return render_template('base.html')
# launch
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
这是我的模板:
base.html文件
<!doctype html>
<html>
<head>
{% block head %}
{# <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> #}
<title>{% block title %}{% endblock %} - My Webpage</title>
{% endblock %}
</head>
<body>
<div id="content">{% block content %}{% endblock %}</div>
<div id="footer">
{% block footer %}
© Copyright 2010 by <a href="http://domain.invalid/">you</a>.
{% endblock %}
</div>
</body>
</html>
index.html
{% extends "base.html" %}
{% block title %} - Home{% endblock %}
{% block content %}
{% if not session.logged_in %}
<p>You are not logged in.</p>
{% else %}
<p>You are logged in.</p>
{% endif %}
{% endblock %}