Пример #1
0
def edit(request, id):
    """
    Description: Edit a given blog post
    
    Arguments:   - request: HttpRequest object
                 - id: Id of Blog Post of interest
    Return:      HttpResponse
                                  
    Author:      Nnoduka Eruchalu
    """
    post = get_object_or_404(Post, id=id)

    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            form.save()
            messages.add_message(request, messages.SUCCESS, "Post successfully edited")
            return HttpResponseRedirect(reverse("postDetail", kwargs={"id": id}))

    # if request.method != 'POST'
    else:
        form = PostForm(instance=post)

    return render_to_response("blog/edit.html", {"form": form}, context_instance=RequestContext(request))
Пример #2
0
def create(request):
    """
    Description: Create a blog post
    
    Arguments:   - request: HttpRequest object
    Return:      HttpResponse
                                  
    Author:      Nnoduka Eruchalu
    """
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.user = request.user  # who is uploading this?
            post.save()  # finally create db record
            form.save_m2m()  # this is to save tags
            messages.add_message(request, messages.SUCCESS, "Post successfully created")
            return HttpResponseRedirect(reverse("posts"))

    else:
        form = PostForm()

    return render_to_response("blog/create.html", {"form": form}, context_instance=RequestContext(request))