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
  Using Flask with thonny for my project website need help Confusednoob 3 1,305 Mar-16-2025, 05:42 PM
Last Post: snippsat
  Moving flask app to sub folder on live site. Help This is driving me crazy **huh** Rosssaab 1 720 Jan-10-2025, 02:11 PM
Last Post: menator01
Question running Flask with waitress having web traffic log... SpongeB0B 3 7,786 Dec-09-2024, 03:34 AM
Last Post: bazinga
  Unable to Generate Class Code in Flask App - Form Not Submitting Correctly misbahskuy 1 904 Dec-08-2024, 07:07 PM
Last Post: snippsat
  Flask web app MySQL runtime error TheTiger 8 5,126 Dec-04-2024, 03:18 AM
Last Post: TheTiger
  Docker Flask App on windows system Osaci 0 879 Oct-27-2024, 06:12 PM
Last Post: Osaci
  Web app with DB Crud can't pip install flask-mysqldb TheTiger 4 2,117 Oct-23-2024, 12:30 AM
Last Post: TheTiger
  How do I capture URL information with Flask? MacAarthur 0 910 Sep-16-2024, 06:37 AM
Last Post: MacAarthur
  Flask_table module compatibility issue: cannot import name 'Markup' from 'flask' venkateshbalagiri 3 2,720 Sep-13-2024, 06:14 AM
Last Post: buran
  Flask basic help needed aArtur 2 902 Sep-09-2024, 06:45 PM
Last Post: aArtur

Forum Jump:

User Panel Messages

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