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:
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:
Comments
Post a Comment