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
  [split] Flask OSError: [Errno 98] Address already in use venkateshbalagiri 9 927 Dec-09-2021, 05:05 PM
Last Post: venkateshbalagiri
  Flask basics... url ifigazsi 1 314 Nov-19-2021, 09:38 PM
Last Post: ifigazsi
  Using Python Embedded with Flask Restplus API : to deploy on IIS gaurav_umang 1 892 Nov-18-2021, 01:35 AM
Last Post: Larz60+
  Upgrade/Implement to FlaskRestPlus from Flask tlopezdh 0 615 Nov-15-2021, 04:38 PM
Last Post: tlopezdh
  [split] flask 500 internal server error DarthTensor 3 928 Nov-11-2021, 06:10 AM
Last Post: DarthTensor
  Flask 404 eroor Annoogy 1 484 Oct-12-2021, 12:27 PM
Last Post: snippsat
  new window in separate thread - flask website Notabene 3 716 Sep-20-2021, 08:29 AM
Last Post: jamesaarr
  IndexError: list index out of range" & "TypeError: The view function f: Flask Web App joelbeater992 5 1,117 Aug-31-2021, 08:08 PM
Last Post: joelbeater992
  How do you define and -put a value into a variable in Flask? a4ginatl 10 2,205 Aug-15-2021, 03:07 PM
Last Post: a4ginatl
  Flask web app on Azure help dangermaus33 2 1,353 Aug-10-2021, 12:04 PM
Last Post: kashcode

Forum Jump:

User Panel Messages

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