简体   繁体   English

如何在将原始数据保留在Django中的同时显示更新表单?

[英]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.

相关问题 如何在 Django 中以更新形式显示数据库中已有的数据? - How to show data already on the database in a update form in Django? 如何从csv文件更新django表中的数据,同时保持主键值不变。 - How to update data in django table from csv file while keeping the primary key value as it is.? 在Django中保持输入表单数据 - Keeping typed in form data in django Django使用原始数据更新模型 - Django update model with original data 如何通过添加来修改Django内容的某些部分<span>,同时保持段落文本的原始格式?</span> - How to modify certain part of Django content by adding <span>, while keeping the original format of paragraph text? 在保持 HTML 样式的同时编写 Django 表单 - Writing a Django Form while keeping HTML styling 如何在使用 Flask 和 WTForms 保留表单数据的同时重定向? - How to redirect while keeping form data using Flask and WTForms? 如何在保持原始数据格式的同时计算 pandas dataframe 中一组列的百分比 - how to calculate the percentage in a group of columns in pandas dataframe while keeping the original format of data 如何从原始mnist数据大小创建样本子集,同时保留所有10个类 - how to creat a subset of sample from original size of mnist data, while keeping all 10 classes django在保持数据的同时将OneToOneField移到ForeignKey - django moving a OneToOneField to a ForeignKey while keeping data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM