jquery - Django - Add field to form dynamically -


objective: add field form dynamically pressing button. once submitted, store values fields in list.

(i have looked @ other questions on stack overflow , tried find basic tutorial/explanation how this: dynamically add field form)

i not sure need looking up/researching. understand involve javascript django side unsure how work.

i have created basic form starters:

enter image description here

models.py

from django.db import models  class simpleform(models.model):     basic_field = models.charfield(max_length=200) 

forms.py

from django import forms models import simpleform  class simpleform(forms.modelform):  class meta:     model = simpleform     fields = ['basic_field'] 

views.py

from django.shortcuts import render forms import simpleform  def index(request):     if request.method == "post":         # loop iterate on dynamically created fields         # store values in list     else:         form = simpleform()     return render(request, "index.html", {'form':form}) 

index.html

{% block content %}     <h1>simple form</h1>     <form method="post" class="post-form">{% csrf_token %}         {{ form.as_p }}         <button type="button" class="btn-default">add field</button>         <button type="submit" class="btn-default">submit</button>     </form> {% endblock %} 

here have snippet without javascript:

https://djangosnippets.org/snippets/10524/


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -