无法更新表单中的所需字段

时间:2018-05-02 14:32:23

标签: python django

因此,当我点击edit按钮时,我会收到预先填好的表格。我试图更新我的一个条目,但我没有成功。

这是我的update观点:

def update(request, id):
    item = get_object_or_404(BookEntry, id=id)
    if request.method=="POST":
        form = UpdateForm(request.POST, instance=item)
        if form.is_valid():
            post=form.save(commit=False)
            post.save()
            return HttpResponseRedirect(reverse('bms:index'), id)
        else:
            form=UpdateForm(instance=item)
            return HttpResponseRedirect(reverse('bms:index'), id)
        return render(request, 'index.html',{'form':form})

这是我的forms.py

from django import forms
from bms.models import BookEntry


    class UpdateForm(forms.ModelForm):
        title = forms.CharField(max_length=100)
        author = forms.CharField(max_length=100)
        edition = forms.CharField(max_length=100)
        publisher = forms.CharField(max_length=100)
        genre = forms.CharField(max_length=100)
        detail = forms.CharField(max_length=100)
        language = forms.CharField(max_length=100)
        price = forms.IntegerField()
        dop = forms.CharField(max_length=100)
        cop = forms.CharField(max_length=100)
        copyright = forms.CharField(max_length=100)
        isbn = forms.IntegerField()

        class Meta:
            model = BookEntry
            fields = '__all__'

0 个答案:

没有答案