简体   繁体   中英

Override POST initial value in Django form field

I have a page with a list of the same forms. Each form has a different value for a hidden field to distinguish the submissions. If I submit one of the forms, the page is refreshed and all the same forms are shown. However, each form now has it's fields populated with the data submitted on the last request. I want each form's fields to be empty no matter what. I tried supplying initial='' to the field in the form object; this didn't work.

Ideas?

Thanks

If you want to use a list of same forms, you should use formsets .

Overriding POST initial values:

form = SomeForm()
if request.method == 'POST':
    form = SomeForm(request.POST)
    if form.is_valid():
        #do something and redirect somewhere
    #next line will be reached only if form is invalid
    #and you want to override POST data
    form = SomeForm()
return render(.....)

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