[Answered ]-Edit form in Django 1.6

2👍

So there are at least three issues here.

Firstly, as I say, your repeated instantiation of the form variable is pointless; it’s as if you never did the first one at all. So don’t.

Secondly, I don’t understand what the point of postOneComment is. You’ve carefully defined a form and instantiated it with an instance, now you’ve defined a separate function that completely ignores the form and saves a comment directly from the POST. This code wouldn’t actually work, in fact, because you try to pass the username string as the user parameter instead of the actual user.

Thirdly, the actual problem you are having is that your form is not valid. However, you don’t show the errors on the template, so you don’t see them – and you also don’t redirect to another URL after a successful post, so you have no way of telling whether the post is successful or not.

The reason your form is not valid is that you explicitly include the user parameter in the form, but then don’t supply it on the template. So that form will never be valid. Clearly you want to set it automatically, so remove it from fields.

So, to summarise:

  • Remove user from the form fields;
  • Add {{ form.errors }} to your template;
  • Replace your view code with this:

.

def edit_comment(request, one_entry):
    content = Comment.objects.get(pk=one_entry)

    if request.method == 'POST':
        form = ReplyForm(request.POST, instance=content)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.user = request.user
            comment.save()
            return redirect('/')  # or wherever you want
    else:
        form = ReplyForm()
    c = {"form":form,"one_entry": content}
    return render(request,"template.html", c)

Leave a comment