简体   繁体   中英

Edit form in Django 1.6

I'm trying to edit a Modelform with an "edit" button but the form isn't updated. I have one view-function, original for posting and saving a new post. Then I have a second function, edit_comment , for updating the object one_entry that is created in original. In my template I have one form for submitting posts and an "edit" button connected to url "/edit" to update the added one_entry objects from the form with url "/entry". Any help is greatly appriciated.

in views

# function for adding a new post
def original(request, postID):
    one_entry = Entry.objects.get(id=postID)

    if request.method == 'POST':
        form = ReplyForm(request.POST)
        if form.is_valid():
            postOneComment(request, one_entry)

    else:
        form = ReplyForm()

    c = {"one_entry":one_entry,"form":form,"comment_views":comment_views}
    c.update(csrf(request))

    return render(request,"template.html", c)


# function for editing/updating the object one_entry
# Comment is a model and content and title are fields in the Modelform 
# that should be updated
def edit_comment(request, one_entry):
    content = Comment.objects.get(pk=one_entry)
    title = Comment.objects.get(pk=one_entry)

    if request.method == 'POST':
        form = ReplyForm(request.POST, instance=content)
        form = ReplyForm(request.POST, instance=title)
        if form.is_valid():
            postOneComment(request, one_entry)

    else:
        form = ReplyForm()

    c = {"form":form,"one_entry":one_entry}
    c.update(csrf(request))

    return render(request,"template.html", c)


def postOneComment(request, one_entry):
    content = request.POST["content"]

    title = request.POST["title"]



    user= request.user.username


    entryComment = Comment(datetime=datetime.datetime.now(), belongsTo=one_entry,content=content, title=title, user=user)
    entryComment.save()

in forms

class ReplyForm(forms.ModelForm):
    title = forms.CharField(max_length=70)
    content = forms.CharField(widget=forms.Textarea)

    class Meta:
        model = Comment
        fields = ("title","content", "user")

in urls

# url for adding a post to object one_entry 
url(r'^entry(?P<postID>\d+)$', original),

# url for editing/updating one_entry  
url(r'^edit(?P<one_entry>\d+)$',edit_comment),

in template

#button for editing one_entry
{% for t in one_entry.comment_set.all %}
    <a type="button" href="/edit{{ t.id}}" class="btn btn-xs   btn-success">EDIT</button></a>

    #form for adding  a new post
    <form  method="post" action="">{% csrf_token %}
        <center>Titel (optional){{ form.title}}</center></br>
        <center>{{ form.content}}</center>
        <center><button type="submit" class="btn btn-success">POST</button></center>
    </form>

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM