0

I am trying to format the date which comes from my sqlite3 database engine. I am using a Flask framework to host the application and I am getting the following error message: 'None Type object is not subcriptable'which may have to do with the way I am formatting my date when pulled from my database. Not sure on what to do here?

     @app.route('/view/<date>', methods=['GET','POST'])#date to be 20200520 format
     def view(date):
          db = get_db()
          cur = db.execute('select entry_date from log_date where entry_date = ?', [date])
          result = cur.fetchone()
          d = datetime.strptime(str(result['entry_date']), '%Y%m%d')
          pretty_date = datetime.strftime(d, '%B %d, %Y')

         return render_template('day.html', date=pretty_date)

The error that I am getting is:

TypeError

TypeError: 'NoneType' object is not subscriptable Traceback (most recent call last)

File "/usr/lib/python3.6/site-packages/flask/app.py", line 2464, in __call__

return self.wsgi_app(environ, start_response)

File "/usr/lib/python3.6/site-packages/flask/app.py", line 2450, in wsgi_app

response = self.handle_exception(e)

File "/usr/lib/python3.6/site-packages/flask/app.py", line 1867, in handle_exception

reraise(exc_type, exc_value, tb)

File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise

raise value

File "/usr/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app

response = self.full_dispatch_request()

File "/usr/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request

rv = self.handle_user_exception(e)

File "/usr/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise

raise value

File "/usr/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request

rv = self.dispatch_request()

File "/usr/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request

return self.view_functions[rule.endpoint](**req.view_args)

File "/root/python_flask_projects/food_tracker_flask_app/app.py", line 59, in view

@app.route('/view/<date>', methods=['GET','POST'])#date to be 20200520 format

def view(date):

        db = get_db()

        cur = db.execute('select entry_date from log_date where entry_date = ?', [date])

        result = cur.fetchone()

        datestring = str(result['entry_date'])

        #d = datetime.strptime(str(result['entry_date']), '%Y%m%d')

        d = datetime.strptime(datestring, '%Y%m%d')

        pretty_date = datetime.strftime(d, '%B %d, %Y')

 

        return render_template('day.html', date=pretty_date)

TypeError: 'NoneType' object is not subscriptable

Thanks for any help that can be provided.

1 Answer 1

1

.fetchone() will return None if no rows matched the query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.