该书第11.3.1节末尾说运行程序会看到首页显示了很长的随机博客文章列表。但是我运行程序却提示出现AttributeError: 'NoneType' object has no attribute 'encode',然后我运行github作者源码,也出现了这个bug(如下)
Traceback (most recent call last):
File "E:\flalearn\venv\lib\site-packages\flask\app.py", line 1997, in call
return self.wsgi_app(environ, start_response)
File "E:\flalearn\venv\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "E:\flalearn\venv\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "E:\flalearn\venv\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "E:\flalearn\venv\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "E:\flalearn\venv\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "E:\flalearn\venv\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "E:\flalearn\venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functionsrule.endpoint
File "E:\flalearn\app\main\views.py", line 22, in index
return render_template('index.html', form = form, posts = posts)
File "E:\flalearn\venv\lib\site-packages\flask\templating.py", line 134, in render_template
context, ctx.app)
File "E:\flalearn\venv\lib\site-packages\flask\templating.py", line 116, in _render
rv = template.render(context)
File "E:\flalearn\venv\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "E:\flalearn\venv\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "E:\flalearn\app\templates\index.html", line 3, in top-level template code
{% import "_macros.html" as macros %}
File "E:\flalearn\app\templates\base.html", line 1, in top-level template code
{% extends "bootstrap/base.html" %}
File "E:\flalearn\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 1, in top-level template code
{% block doc -%}
File "E:\flalearn\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 4, in block "doc"
{%- block html %}
File "E:\flalearn\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 20, in block "html"
{% block body -%}
File "E:\flalearn\venv\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 23, in block "body"
{% block content -%}
File "E:\flalearn\app\templates\base.html", line 62, in block "content"
{% block page_content %}{% endblock %}
File "E:\flalearn\app\templates\index.html", line 16, in block "page_content"
{% include '_posts.html' %}
File "E:\flalearn\app\templates_posts.html", line 6, in top-level template code
File "E:\flalearn\app\models.py", line 184, in gravatar
self.email.encode('utf-8')).hexdigest()
AttributeError: 'NoneType' object has no attribute 'encode'
原因:练习前面章节时向data-dev.sqlite添加了用户,但对这些用户没设置邮箱,导致User类的gravatar方法处运行出现错误。解决办法1:设置if self.email(如下)。方法2:在数据库中给用户手动设置邮箱。
def gravatar(self, size = 100, default = 'identicon', rating = 'g'):
if request.is_secure:
url = 'https://secure.gravatar.com/avatar'
else:
url = 'http://www.gravatar.com/avatar'
if self.email:
hash = self.avatar_hash or hashlib.md5(
self.email.encode('utf-8')).hexdigest()
return '{url}/{hash}?s={size}&d={default}&r={rating}'.format(
url=url, hash=hash, size=size, default=default, rating=rating)