1. ホーム
  2. パイソン

flask web development create virtual blog post data bug AttributeError: 'NoneType' object has no attribute 'encode' solution

2022-02-24 14:23:23

本の11.3.1項の末尾に、このプログラムを実行すると、トップページにランダムなブログ記事の長いリストが表示されると書いてあります。しかし、プログラムを実行すると AttributeError: 'NoneType' object has no attribute 'encode' と表示され、さらに github author のソースコードを実行すると、このバグも発生しました(以下のように)。

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)