Project 4: Movie Watchlist: Editing movies and pre-populating a form (+53, -3)
routes.py (+20, -1)
From:
curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/start/movie_library/routes.py
To:
curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/end/movie_library/routes.py
index 110e26f..8ebf9ec 100644
--- a/curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/start/movie_library/routes.py
+++ b/curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/end/movie_library/routes.py
@@ -11,7 +11,7 @@ from flask import (
url_for,
request,
)
-from movie_library.forms import MovieForm
+from movie_library.forms import MovieForm, ExtendedMovieForm
from movie_library.models import Movie
@@ -59,6 +59,25 @@ def movie(_id: str):
return render_template("movie_details.html", movie=movie)
+@pages.route("/edit/<string:_id>", methods=["GET", "POST"])
+def edit_movie(_id: str):
+ movie = Movie(**current_app.db.movie.find_one({"_id": _id}))
+ form = ExtendedMovieForm(obj=movie)
+ if form.validate_on_submit():
+ movie.title = form.title.data
+ movie.director = form.director.data
+ movie.year = form.year.data
+ movie.cast = form.cast.data
+ movie.series = form.series.data
+ movie.tags = form.tags.data
+ movie.description = form.description.data
+ movie.video_link = form.video_link.data
+
+ current_app.db.movie.update_one({"_id": movie._id}, {"$set": asdict(movie)})
+ return redirect(url_for(".movie", _id=movie._id))
+ return render_template("movie_form.html", movie=movie, form=form)
+
+
@pages.get("/movie/<string:_id>/watch")
def watch_today(_id):
current_app.db.movie.update_one(
movie_details.html (+2, -2)
From:
curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/start/movie_library/templates/movie_details.html
To:
curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/end/movie_library/templates/movie_details.html
index 6edb906..5828c20 100644
--- a/curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/start/movie_library/templates/movie_details.html
+++ b/curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/end/movie_library/templates/movie_details.html
@@ -30,7 +30,7 @@
{% else %}
<p><a href="{{ url_for('pages.watch_today', _id=movie._id) }}" class="watched__link">Not watched yet</a></p>
{% endif %}
- <a class="movie__edit" href="#">Edit {{ pencil("pencil") }}</a>
+ <a class="movie__edit" href="{{ url_for('pages.edit_movie', _id=movie._id) }}">Edit {{ pencil("pencil") }}</a>
</div>
</div>
<div class="header__row">
@@ -55,7 +55,7 @@
{% if movie.description %}
<p class="movie__description">{{ movie.description }}</p>
{% else %}
- <p class="movie__description">No description yet. <a class="link" href="#">Add one?</a></p>
+ <p class="movie__description">No description yet. <a class="link" href="{{ url_for('pages.edit_movie', _id=movie._id) }}">Add one?</a></p>
{% endif %}
<div class="movie__meta">
movie_form.html (+31, -0)
From:
curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/end/movie_library/templates/movie_form.html
To:
curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/end/movie_library/templates/movie_form.html
new file mode 100644
index 0000000..293efe3
--- /dev/null
+++ b/curriculum/section14/lectures/14_edit_movies_pre_populate_wtform/end/movie_library/templates/movie_form.html
@@ -0,0 +1,31 @@
+{% from "macros/fields.html" import render_text_field, render_area_field %}
+
+{% extends "layout.html" %}
+
+{%- block head_content %}
+ <link rel="stylesheet" href="{{ url_for('static', filename='css/forms.css') }}" />
+{% endblock %}
+
+{% block main_content %}
+
+ <form name="add_movie" method="post" novalidate class="form">
+ <div class="form__container">
+ {{ form.hidden_tag() }}
+ {{ render_text_field(form.title) }}
+ {{ render_text_field(form.director) }}
+ {{ render_text_field(form.year) }}
+
+ {{ render_area_field(form.cast) }}
+ {{ render_area_field(form.series) }}
+ {{ render_area_field(form.tags) }}
+
+ {{ render_area_field(form.description) }}
+ {{ render_text_field(form.video_link) }}
+
+ <div>
+ {{ form.submit(class_="button button--form") }}
+ </div>
+ </div>
+ </form>
+
+{% endblock %}