Getting strange TypeError from my function
I'm getting an error (TypeError: add_movie() got an unexpected keyword argument 'title'
) on a function of mine and can't figure out why. I'm assuming I'm missing something stupid/small again. I can see that the function does indeed have the named param title
. Every other similar question I've read was missing the offending param from the function - this one, however, is not.
All of the code below is in the same file.
main.py:
from flask import Flask, render_template, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, Integer, String, Float
URL_MOVIE_META = 'https://api.themoviedb.org/3/movie'
app = Flask(__name__)
app.config['SECRET_KEY'] = "It's a secret"
db = SQLAlchemy(app)
class Movie(db.Model):
__tablename__ = 'movie'
id = Column(Integer, primary_key=True)
title = Column(String(250), unique=True, nullable=False)
year = Column(Integer, nullable=False)
description = Column(String(250), nullable=False)
rating = Column(Float, nullable=False)
ranking = Column(Integer, nullable=False)
review = Column(String(250), nullable=False)
img_url = Column(String(250))
def add_movie(title, year, description, rating, ranking, review, img_url):
global db
movie = Movie(
title=title,
year=year,
description=description,
rating=rating,
ranking=ranking,
review=review,
img_url=img_url
)
db.session.add(movie)
db.session.commit()
...
def get_metadata_for_movie(tmdb_id):
params = {
"api_key": TMDB_API_KEY,
}
response = requests.get(f'{URL_MOVIE_META}/{tmdb_id}', params=params)
data = response.json()
year = datetime.strptime(data['release_date'], '%Y-%m-%d').year
add_movie(
title=data['title'],
year=year,
description=data['overview'],
rating=0,
ranking=0,
review='',
img_url=f'{URL_POSTER_PREFIX}/{data["poster_path"]}'
)
...
@app.route("/")
def home():
movies = get_all_movies()
return render_template("index.html", movies=movies)
@app.route('/add/<tmdb_id>')
def add_movie(tmdb_id):
get_metadata_for_movie(tmdb_id)
print(f'{tmdb_id}')
# Add movie...?
return redirect(url_for('home'))
The error:
TypeError: add_movie() got an unexpected keyword argument 'title'
EDIT: added more example code
Solution 1:
The second add_movie function is overriding the first one, so Python tells you that the current definition doesn't accept a keyword argument named "title" - and it's true, since the only argument is "tmbd_id".