Python Forum
python 3.7 on windows using flask and flask-sqlalchemy.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python 3.7 on windows using flask and flask-sqlalchemy.
#1
[url=https://github.com/celiao/tmdbsimple/]API used[/url]
#I can't extract data from an API arrow and add it to my database

@blueprintRote.route('/', methods =['GET','POST'])
def post_movies():
    #POST
    if  request.method == 'POST':
        def search(title):
            search = tmdb.Search()
            response = search.movie(query=title)
            info = json.dumps(search.results)
            record = []
            for infos in info:
                record.append((infos['title'], infos['release_date'], infos['popularity']))
            return record
   
        m = search('The Bourne')
        
        new_user = Movies(title=m['title'],
         release_date=m['release_date'],popularity=m['popularity'])

        db.session.add(new_user)
        db.session.commit()

    return render_template('myMovies.htm')
Error:
File "c:\users\alefg\projects\mytop100movies\venv\lib\site-packages\flask\app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "C:\Users\alefg\projects\MyTop100Movies\src\blueprint\blueprintsRoutes.py", line 21, in post_movies m = search('The Bourne') File "C:\Users\alefg\projects\MyTop100Movies\src\blueprint\blueprintsRoutes.py", line 18, in search record.append((infos['title'], infos['release_date'], infos['popularity'])) TypeError: string indices must be integers
Reply
#2
with this line:
info = json.dumps(search.results)
you serialize search.results to JSON formatted string and then later here:
record.append((infos['title'], infos['release_date'], infos['popularity']))
you try to access it's keys like it's dictionary, which causes the error shown
Reply
#3
def search(title):
          search = tmdb.Search()
          response = search.movie(query=title)
          record = []
          for info in search.results:
               record.append(info['title'])
               record.append(info['release_date'])
               record.append(info['popularity'])
          return record
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  new window in separate thread - flask website Notabene 0 64 Sep-15-2021, 06:41 AM
Last Post: Notabene
  IndexError: list index out of range" & "TypeError: The view function f: Flask Web App joelbeater992 5 270 Aug-31-2021, 08:08 PM
Last Post: joelbeater992
  How do you define and -put a value into a variable in Flask? a4ginatl 10 1,134 Aug-15-2021, 03:07 PM
Last Post: a4ginatl
  Flask web app on Azure help dangermaus33 2 962 Aug-10-2021, 12:04 PM
Last Post: kashcode
Question Deploy Flask apps on Windows... SpongeB0B 2 281 Aug-08-2021, 09:12 AM
Last Post: ndc85430
  Flask TypeError: Object of type Decimal is not JSON serializable mekacharan 0 574 Jul-15-2021, 05:28 AM
Last Post: mekacharan
  Flask read real time print from subprocess without deadlock vofka32 0 504 Jun-02-2021, 09:36 AM
Last Post: vofka32
Question running Flask with waitress having web traffic log... SpongeB0B 2 883 May-06-2021, 07:07 AM
Last Post: SpongeB0B
Question problems with flask and celery Phantomsec 2 1,471 Apr-28-2021, 07:30 PM
Last Post: Phantomsec
  Error updating one to many relationship in Flask/ SQLAlchemy atindra 0 879 Apr-15-2021, 10:29 PM
Last Post: atindra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020