記錄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開發實戰:入門、進階與原理解析(李輝著 )》一書

我個人覺得這本書挺好的,講得詳細易懂。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM