miguelgrinberg / microblog

The microblogging application developed in my Flask Mega-Tutorial series. This version maps to the 2024 Edition of the tutorial.
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
MIT License
4.56k stars 1.65k forks source link

flask_moment error when trying to format #362

Closed joshgalvan closed 10 months ago

joshgalvan commented 10 months ago

this causes me to get the error "str object has no attribute 'strftime'"

Full stacktrace:

Traceback (most recent call last):
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask/app.py", line 1478, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask/app.py", line 1458, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask/app.py", line 1455, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask/app.py", line 869, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask/app.py", line 867, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask/app.py", line 852, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask_login/utils.py", line 290, in decorated_view
    return current_app.ensure_sync(func)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/<redacted>/microblog/app/routes.py", line 151, in user
    return render_template('user.html', user=user, posts=posts.items,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask/templating.py", line 152, in render_template
    return _render(app, template, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask/templating.py", line 133, in _render
    rv = template.render(context)
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "/home/<redacted>/microblog/app/templates/user.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "/home/<redacted>/microblog/app/templates/base.html", line 58, in top-level template code
    {% block content %}{% endblock %}
  File "/home/<redacted>/microblog/app/templates/user.html", line 11, in block 'content'
    <p>{{ _('Last seen on') }}: {{ moment(user.last_seen).format('LLL') }}</p>
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask_moment/__init__.py", line 212, in format
    return self._render("format", format=(fmt or ''), refresh=refresh)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask_moment/__init__.py", line 186, in _render
    t = self._timestamp_as_iso_8601(self.timestamp)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/<redacted>/python3.11/site-packages/flask_moment/__init__.py", line 182, in _timestamp_as_iso_8601
    return timestamp.strftime('%Y-%m-%dT%H:%M:%S' + tz)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'strftime'
joshgalvan commented 10 months ago

This error is also visible in my version of Chapter 11. I've copied everything by hand (mostly) to write it myself but can't find anything wrong with my code causing this to happen. Especially when comparing it to yours, it looks the same.

joshgalvan commented 10 months ago

I also tried updating to python 3.12 and the issue still persists

miguelgrinberg commented 10 months ago

What's the type of user.last_seen?

joshgalvan commented 10 months ago

Good catch. My User.last_seen variable was typed as str instead datetime. The pains of copying tutorials lol. Fixed!