Skip to content

Instantly share code, notes, and snippets.

@aliceridgway
Last active June 28, 2022 20:49
Show Gist options
  • Save aliceridgway/a4547d497c5accb13553e6dc05aaa69f to your computer and use it in GitHub Desktop.
Save aliceridgway/a4547d497c5accb13553e6dc05aaa69f to your computer and use it in GitHub Desktop.
edit_post
def edit_post(request: HttpRequest, slug: str) -> HttpResponse:
post = get_object_or_404(Post, slug=slug)
form = PostForm(instance=post)
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
post = form.save()
return redirect("post_detail", slug=post.slug)
context = {"form": form, "post": post, "edit_mode": True}
return render(request, "post_form.html", context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment