title: TypeError: Parser must be a string or character stream, not datetime
body: <p>I'm trying to have a webpage show all of the upcoming shows listed in a database, along with a bunch of info about each show that's stored in venue and artist tables respectively.  I query the Show table, and then for each row in that result I link the Artist and Venue tables.  Because the error has something to do with datetime, I'm assuming the error is something to do with either <code>if show.start_time &gt; current_time:</code> or <code>&quot;start_time&quot;: show.start_time</code> but the error doesn't pin down where it is.  I tried converting both current_time and show.start_time to a string just to see if that worked, but no luck.  What am I doing wrong?</p>
  <pre><code>@app.route('/shows')
  def shows():
  
    current_time = datetime.now()
  
    data = []
  
    allshows = Show.query.all()
  
    for show in allshows:
      artist = Artist.query.get(show.artist_id)
      venue = Venue.query.get(show.venue_id)
  
      if show.start_time &gt; current_time:
        data.append({
          &quot;venue_id&quot;: show.venue_id,
          &quot;venue_name&quot;: venue.name,
          &quot;artist_id&quot;: show.artist_id,
          &quot;artist_name&quot;: artist.name,
          &quot;artist_image_link&quot;: artist.image_link,
          &quot;start_time&quot;: show.start_time
        })
   
    return render_template('pages/shows.html', shows=data)
  </code></pre>
  <p>If it helps, here are the models associated with it:</p>
  <pre><code>class Venue(db.Model):
      __tablename__ = 'venues'
  
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String)
      city = db.Column(db.String(120))
      state = db.Column(db.String(120))
      address = db.Column(db.String(120))
      phone = db.Column(db.String(120))
      image_link = db.Column(db.String(500))
      facebook_link = db.Column(db.String(120))
      genres = db.Column(db.ARRAY(db.String))
      image_link = db.Column(db.String(500))
      facebook_link = db.Column(db.String(120))
      website = db.Column(db.String(300))
      seeking_talent = db.Column(db.Boolean)
      seeking_description = db.Column(db.String(120))
      shows = db.relationship('Show', backref='venue', lazy=True)
  
  class Artist(db.Model):
      __tablename__ = 'artists'
  
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String)
      city = db.Column(db.String(120))
      state = db.Column(db.String(120))
      phone = db.Column(db.String(120))
      genres = db.Column(db.String(120))
      image_link = db.Column(db.String(500))
      facebook_link = db.Column(db.String(120))
      website = db.Column(db.String(300))
      seeking_shows = db.Column(db.Boolean)
      seeking_description = db.Column(db.String(120))
      shows = db.relationship('Show', backref='artist', lazy=True)        
  
  class Show(db.Model):
      __tablename__ = 'shows'
  
      id = db.Column(db.Integer, primary_key=True)
      artist_id = db.Column(db.Integer, db.ForeignKey('artists.id'), nullable = False)
      venue_id = db.Column(db.Integer, db.ForeignKey('venues.id'), nullable = False)
      start_time = db.Column(db.DateTime, nullable = False)
  
      def __repr__(self):
          return '&lt;Show {} {}&gt;'.format(self.artist_id, self.venue_id)
  </code></pre>
  <p>Edit: Including full traceback.</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/app.py&quot;, line 2464, in __call__
      return self.wsgi_app(environ, start_response)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/app.py&quot;, line 2450, in wsgi_app
      response = self.handle_exception(e)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/app.py&quot;, line 1867, in handle_exception
      reraise(exc_type, exc_value, tb)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/_compat.py&quot;, line 39, in reraise
      raise value
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/app.py&quot;, line 2447, in wsgi_app
      response = self.full_dispatch_request()
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/app.py&quot;, line 1952, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/app.py&quot;, line 1821, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/_compat.py&quot;, line 39, in reraise
      raise value
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/app.py&quot;, line 1950, in full_dispatch_request
      rv = self.dispatch_request()
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/app.py&quot;, line 1936, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/app.py&quot;, line 547, in shows
      return render_template('pages/shows.html', shows=data)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/templating.py&quot;, line 137, in render_template
      return _render(
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/flask/templating.py&quot;, line 120, in _render
      rv = template.render(context)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/jinja2/environment.py&quot;, line 1090, in render
      self.environment.handle_exception()
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/jinja2/environment.py&quot;, line 832, in handle_exception
      reraise(*rewrite_traceback_stack(source=source))
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/jinja2/_compat.py&quot;, line 28, in reraise
      raise value.with_traceback(tb)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/templates/pages/shows.html&quot;, line 1, in top-level template code
      {% extends 'layouts/main.html' %}
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/templates/layouts/main.html&quot;, line 103, in top-level template code
      {% block content %}{% endblock %}
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/templates/pages/shows.html&quot;, line 9, in block &quot;content&quot;
      &lt;h4&gt;{{ show.start_time|datetime('full') }}&lt;/h4&gt;
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/app.py&quot;, line 101, in format_datetime
      date = dateutil.parser.parse(value)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/dateutil/parser.py&quot;, line 1168, in parse
      return DEFAULTPARSER.parse(timestr, **kwargs)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/dateutil/parser.py&quot;, line 556, in parse
      res, skipped_tokens = self._parse(timestr, **kwargs)
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/dateutil/parser.py&quot;, line 675, in _parse
      l = _timelex.split(timestr)         # Splits the timestr into tokens
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/dateutil/parser.py&quot;, line 192, in split
      return list(cls(s))
    File &quot;/Users/ZEMEL/Dropbox (Personal)/Code/fyyurr/env/lib/python3.8/site-packages/dateutil/parser.py&quot;, line 60, in __init__
      raise TypeError('Parser must be a string or character stream, not '
  TypeError: Parser must be a string or character stream, not datetime
  </code></pre>
  <p>Edit 2: <code>def</code> that formats datetime:</p>
  <pre><code>def format_datetime(value, format='medium'):
    date = dateutil.parser.parse(value)
    if format == 'full':
        format=&quot;EEEE MMMM, d, y 'at' h:mma&quot;
    elif format == 'medium':
        format=&quot;EE MM, dd, y h:mma&quot;
    return babel.dates.format_datetime(date, format)
  
  app.jinja_env.filters['datetime'] = format_datetime
  </code></pre>