[英]How to show an update form while keeping the original data in Django?
I'm using Django Forms to do some updating work. 我正在使用Django Forms做一些更新工作。 For example, I write a
FilmForm
to present a film information form: 例如,我编写了
FilmForm
来呈现电影信息表:
class FilmForm(forms.Form):
name = forms.CharField()
director = forms.CharField()
release_year = forms.DateField()
......(many other infos)
And I create and save a model instance using the form. 然后,我使用表单创建并保存模型实例。 Next time I want to modify only one info, like 'director', and update the model info using the
FilmForm
. 下次,我只想修改一个信息(例如“导演”),并使用
FilmForm
更新模型信息。
def FilmUpdate(request,pk):
if request.method == 'POST':
...
else:
form = FilmForm()
return render(request,'film_update.html', {'form':form})
but when the form show up, the html inputs are all blank,and have to input all the original data again while the only item I want modify is just 'director'. 但是当表单显示时,html输入全部为空白,并且不得不再次输入所有原始数据,而我要修改的唯一项只是“ director”。 So how to present the update form with the original data keeping?
那么,如何在保留原始数据的情况下显示更新表单呢?
def FilmUpdate(request,pk):
instance = Film.objects.get(id=pk)
if request.method == 'POST':
form = FilmForm(instance=instance, data=request.POST, files=request.FILES)
...
else:
form = FilmForm(instance=instance)
return render(request,'film_update.html', {'form':form})
you should pass instance
arg to FilmForm
您应该将
instance
arg传递给FilmForm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.