main.py:
return render_template("edit.html",
movie=movie_to_update, form=form)
index.html: {{ wtf.quick_form(form) }}
main.py
@app.route("/edit", methods=["GET", "POST"])
def update():
"""GET: (before) to render to
specific movie's editing page"""
"""POST(Validated): Updates the rating and
Redirects to home to show a new rating"""
form = UpdateForm()
movie_id = request.args.get("id")
movie_to_update = Movie.query.get(movie_id)
if form.validate_on_submit():
movie_to_update.rating = form.rating.data
movie_to_update.review = form.review.data
db.session.commit()
return redirect(url_for('home'))
return render_template("edit.html",
movie=movie_to_update, form=form)
index.html
<a href="{{ url_for('update',
id=movie.id)}}" class="button">Update</a>
main.py
@app.route("/delete")
def delete():
movie_id = request.args.get("id")
movie_to_delete = Movie.query.get(movie_id)
db.session.delete(movie_to_delete)
db.session.commit()
return redirect(url_for('home'))
index.html
<a href="{{ url_for('delete', id=movie.id)}}"
class="button delete-button">Delete</a>
API call
- Use 'request.args' to get "parsed contents of query string"
- request.args.get("id") gets hold of the value of id from the URL.
- From 'select.html' page:
{{ url_for('find', id=movie.id) }}
url_for with 'id', creates a queary string. e.g. /edit?id=3- At 'main.py':
request.args.get("id")
got hold of query string value.
select.html
<a href="{{ url_for('find', id=movie.id) }} ">{{ movie.title }} - {{movie.release_date}}</a>
main.py
movie_api_id = request.args.get("id")
movie_api_id variable is from select.html :
{{ url_for('find', id=movie.id) }}
== movie api id
id=new_movie.id is for DB id. == id from DB
@app.route("/find")
def find():
movie_api_id = request.args.get("id") #from select.html
if movie_api_id:
parameters = {
"api_key": API_KEY
}
detail_response = requests.get(f"https://api.themoviedb.org/3/movie/{movie_api_id}", params=parameters)
detail_response.raise_for_status()
detail_data = detail_response.json()
#CREATE RECORD
new_movie = Movie(
title=detail_data['title'],
year=detail_data['release_date'].split("-")[0],
img_url=f"https://www.themoviedb.org/t/p/w600_and_h900_bestv2/{detail_data['poster_path']}",
description=detail_data['overview']
)
db.session.add(new_movie)
db.session.commit()
return redirect(url_for('update', id=new_movie.id))
- Innitially, From Add(), if form is validated,
return render_template("select.html", data=result)
- from select.html, go to find()
<a href="{{ url_for('find', id=movie['id'])}}"> {{movie['title']}} - {{movie['release_date']}}</a>
- find(): sort the movies, updated to DB,
return redirect(url_for('update', id=new_movie.id))
- update(): redirect to update form to fill in rating and review
@app.route("/find")
def find():
movie_api_id = request.args.get("id")
if movie_api_id:
parameters = {
"api_key": API_KEY
}
detail_response = requests.get(f"https://api.themoviedb.org/3/movie/{movie_api_id}", params=parameters)
detail_response.raise_for_status()
detail_data = detail_response.json()
#CREATE RECORD
new_movie = Movie(
title=detail_data['title'],
year=detail_data['release_date'].split("-")[0],
img_url=f"https://www.themoviedb.org/t/p/w600_and_h900_bestv2/{detail_data['poster_path']}",
description=detail_data['overview']
)
db.session.add(new_movie)
db.session.commit()
return redirect(url_for('update', id=new_movie.id))
Home
Add
Select
Updating Specific Movie's Rating and Review
Sorted in order