I'm using Django Forms to do some updating work. For example, I write a FilmForm
to present a film information form:
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
.
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'. 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
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.