@login_required
def edit(request, id, form_class=BlogForm, template_name="blog/edit.html"):
    post = get_object_or_404(Post, id=id)
    
    if request.method == "POST":
        if post.author != request.user:
            messages.add_message(request, messages.ERROR,
                ugettext("You can't edit posts that aren't yours")
            )
            return HttpResponseRedirect(reverse("blog_list_yours"))
        if request.POST["action"] == "update":
            blog_form = form_class(request.user, request.POST, instance=post)
            if blog_form.is_valid():
                blog = blog_form.save(commit=False)
                blog.save()
                messages.add_message(request, messages.SUCCESS,
                    ugettext("Successfully updated post '%s'") % blog.title
                )
                return HttpResponseRedirect(reverse("blog_list_yours"))
        else:
            blog_form = form_class(instance=post)
    else:
        blog_form = form_class(instance=post)
    
    return render_to_response(template_name, {
        "blog_form": blog_form,
        "post": post,
    }, context_instance=RequestContext(request))
dataset/ETHPy150Open amarandon/smeuhsocial/apps/blog/views.py/edit/original