Variable number of inputs with Django forms possible?

前端 未结 3 1162
無奈伤痛
無奈伤痛 2020-12-24 08:58

Is it possible to have a variable number of fields using django forms?

The specific application is this:

A user can upload as many pictures as they want on t

3条回答
  •  囚心锁ツ
    2020-12-24 09:47

    If you run

    python manage.py shell
    

    and type:

    from app.forms import PictureForm
    p = PictureForm()
    p.fields
    type(p.fields)
    

    you'll see that p.fields is a SortedDict. you just have to insert a new field. Something like

    p.fields.insert(len(p.fields)-2, 'fieldname', Field())
    

    In this case it would insert before the last field, a new field. You should now adapt to your code.

    Other alternative is to make a for/while loop in your template and do the form in HTML, but django forms rock for some reason, right?

提交回复
热议问题