Update a post if the current user is the author.
(id)
| 86 | @bp.route(class="st">"/<int:id>/update", methods=(class="st">"GET", class="st">"POST")) |
| 87 | @login_required |
| 88 | def update(id): |
| 89 | class="st">""class="st">"Update a post if the current user is the author."class="st">"" |
| 90 | post = get_post(id) |
| 91 | |
| 92 | if request.method == class="st">"POST": |
| 93 | title = request.form[class="st">"title"] |
| 94 | body = request.form[class="st">"body"] |
| 95 | error = None |
| 96 | |
| 97 | if not title: |
| 98 | error = class="st">"Title is required." |
| 99 | |
| 100 | if error is not None: |
| 101 | flash(error) |
| 102 | else: |
| 103 | db = get_db() |
| 104 | db.execute( |
| 105 | class="st">"UPDATE post SET title = ?, body = ? WHERE id = ?", (title, body, id) |
| 106 | ) |
| 107 | db.commit() |
| 108 | return redirect(url_for(class="st">"blog.index")) |
| 109 | |
| 110 | return render_template(class="st">"blog/update.html", post=post) |
| 111 | |
| 112 | |
| 113 | @bp.route(class="st">"/<int:id>/delete", methods=(class="st">"POST",)) |