记录flask使用模板时出现的“Internal Server Error”错误

在看《Flask Web开发实战:入门、进阶与原理解析(李辉著 )》时照着书上的代码抄了一遍,然后运行时发现一直出现以下的错误

书上的源代码如下

watchlist.html

<head>
    <meta charset="utf-8">
    <title>{{ user.username }}'s Watchlist</title>
</head>
    <body>
        <a href="{{ url_for('index') }}">&larr; Return</a>
        <h2>{{ user.username }}</h2>
        {% if user.bio %}
            <i>{{ user.bio }}</i>
        {% else %}
            <i>This user has not provided a bio.</i>
        {% endif %}
        {# Below is the movie list (this is comment) #}
        {<h5>{{ user.username }}'s Watchlist({{ movies|length }}):</h5>
        <ul>
            {% for movie in movies %}
                <li>{{ movie.name }} - {{ movie.year }}</li>
            {% endfor %}
        </ul>
     </body>
</html>

app.py

 1 user = {
 2     'username': 'Grey Li',
 3     'bio': 'A boy who loves movies and music.',
 4 }
 5 
 6 movies = [
 7     {'name': 'My Neighbor Totoro', 'year': '1988'},
 8     {'name': 'Three Colours trilogy', 'year': '1993'},
 9     {'name': 'Forrest Gump', 'year': '1994'},
10     {'name': 'Perfect Blue', 'year': '1997'},
11     {'name': 'The Matrix', 'year': '1999'},
12     {'name': 'Memento', 'year': '2000'},
13     {'name': 'The Bucket list', 'year': '2007'},
14     {'name': 'Black Swan', 'year': '2010'},
15     {'name': 'Gone Girl', 'year': '2014'},
16     {'name': 'CoCo', 'year': '2018'},
17 ]
18 
19 from flask import Flask,render_template,url_for
20 app = Flask(__name__)
21 @app.route('/watchlist')
22 def watchlist():
23     return render_template('watchlist.html', user=user, movies=movies)
24 
25 
26 @app.route('/')
27 def index():
28     return render_template('index.html')
29     
30 '''
31 
32 if __name__=='__main__':
33     app.run()
34 '''

控制台还输入了“werkzeug.routing.BuildError: Could not build url for endpoint 'index.html'. Did you mean 'index' instead?”的错误提示,看不懂

在谷歌有很多类似这样的错误提示的解决办法,但都不是我要找的。刚开始以为是在app.py中没有加上32、33行。

后来才发现应该是在watchlist中加上了 <!-- 注释--> 的内容,大概有几行吧,然后删去注释,重新 flask run 再在浏览器中打开就可以了。

所以有个疑惑,在模板中的貌似不能像在html中那样使用 <!-- 注释-->的格式了。

说明:代码源于《Flask Web开发实战:入门、进阶与原理解析(李辉著 )》一书

我个人觉得这本书挺好的,讲得详细易懂。

原文地址:https://www.cnblogs.com/Guhongying/p/10062525.html