miguelgrinberg / flasky

Companion code to my O'Reilly book "Flask Web Development", second edition.
MIT License
8.52k stars 4.2k forks source link

Error with src="{{ post.author.gravatar(size=40) }}" breaks the application. #518

Closed michaelmiscanuk closed 3 years ago

michaelmiscanuk commented 3 years ago

Hi,

somewhere on version, 11.d flasky got broken with traceback below.

I discovered that it is on this line in _posts.html: <img class="img-rounded profile-thumbnail" src="{{ post.author.gravatar(size=40) }}">

to be specific in calling function gravatar. My thought was that there is some problem with post.author being None.

I wasn't able to fix it, only to remove the src attribute - to make it work, but now all posts have no icon.

Can you help, please?

Thanks.

jinja2.exceptions.UndefinedError
jinja2.exceptions.UndefinedError: 'None' has no attribute 'gravatar'

Traceback (most recent call last)
File "C:\flask_env\Lib\site-packages\flask\app.py", line 2088, in __call__
return self.wsgi_app(environ, start_response)
File "C:\flask_env\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
response = self.handle_exception(e)
File "C:\flask_env\Lib\site-packages\flask\app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "C:\flask_env\Lib\site-packages\flask\app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\flask_env\Lib\site-packages\flask\app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "C:\flask_env\Lib\site-packages\flask\app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "E:\OneDrive\Knowledge Base\0145 Python\Books\Flask\Flask Web Development\code_commits\flasky-11d\app\main\views.py", line 25, in index
return render_template('index.html', form=form, posts=posts,
File "C:\flask_env\Lib\site-packages\flask\templating.py", line 147, in render_template
return _render(
File "C:\flask_env\Lib\site-packages\flask\templating.py", line 128, in _render
rv = template.render(context)
File "C:\flask_env\Lib\site-packages\jinja2\environment.py", line 1304, in render
self.environment.handle_exception()
File "C:\flask_env\Lib\site-packages\jinja2\environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "E:\OneDrive\Knowledge Base\0145 Python\Books\Flask\Flask Web Development\code_commits\flasky-11d\app\templates\index.html", line 3, in top-level template code
{% import "_macros.html" as macros %}
File "E:\OneDrive\Knowledge Base\0145 Python\Books\Flask\Flask Web Development\code_commits\flasky-11d\app\templates\base.html", line 1, in top-level template code
{% extends "bootstrap/base.html" %}
File "C:\flask_env\Lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 1, in top-level template code
{% block doc -%}
File "C:\flask_env\Lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 4, in block 'doc'
{%- block html %}
File "C:\flask_env\Lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 20, in block 'html'
{% block body -%}
File "C:\flask_env\Lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 23, in block 'body'
{% block content -%}
File "E:\OneDrive\Knowledge Base\0145 Python\Books\Flask\Flask Web Development\code_commits\flasky-11d\app\templates\base.html", line 62, in block 'content'
{% block page_content %}{% endblock %}
File "E:\OneDrive\Knowledge Base\0145 Python\Books\Flask\Flask Web Development\code_commits\flasky-11d\app\templates\index.html", line 16, in block 'page_content'
{% include '_posts.html' %}
File "E:\OneDrive\Knowledge Base\0145 Python\Books\Flask\Flask Web Development\code_commits\flasky-11d\app\templates\_posts.html", line 6, in top-level template code
<img class="img-rounded profile-thumbnail" src="{{ post.author.gravatar(size=40) }}">
File "C:\flask_env\Lib\site-packages\jinja2\utils.py", line 84, in from_obj
if hasattr(obj, "jinja_pass_arg"):
jinja2.exceptions.UndefinedError: 'None' has no attribute 'gravatar'
miguelgrinberg commented 3 years ago

This is not a problem with the application. The code uses post.author.avatar() to get the avatar for the author of the given post. Your database has a blog post that has no author set, and for that reason you get this crash. If you set the author for all blog posts that are currently set to None author this problem will go away.

michaelmiscanuk commented 3 years ago

Hi, ok, I might have a little confusion here. I used the post generation from the book and now i have all columns filled. I am not sure if by "author" you mean column author_id - and those are all filled:

image

image

as you can see, it is sorted and there is no NULL. So I am not sure this is it. Thank you.

miguelgrinberg commented 3 years ago

Are all those author IDs valid? Do you have authors created for those IDs?

This is actually easy to figure out. You can just loop through your posts in Python and find the one(s) for which post.author is None. Looking at it from the database is okay I guess, but it is not directly what your code is seeing.

michaelmiscanuk commented 3 years ago

Yep. You were totally right. Because I created some users before generating, then deleted some, then generated, I had users starting from ID 4 to 104, but posts started from id 1 to 100. So I missed users 1, 2, 3. Thank you for helping me debug this.

miguelgrinberg commented 3 years ago

Great. Glad you figured it out.