A
A
Alexander Lyubimov2019-10-04 16:49:09
Django
Alexander Lyubimov, 2019-10-04 16:49:09

How to create dynamic forms in django?

I'm building a web application for my hand-to-hand combat club and doing development in Django for the first time.
I have models that I have migrated to the database. The database contains information on each member of the club.
models.py

class Post(models.Model):
   firstname = models.CharField(max_length=100)
   lastname = models.CharField(max_length=100)
   adult = models.BooleanField(default=False)

Here is the form for creating a new member.
forms.py
class CreateForm(forms.Form):
   firstname = forms.CharField(max_length=60, label="Укажите имя ")
   lastname = forms.CharField(max_length=60, label="Укажите фамилию ")

views.py
def index(request):
   createForm = CreateForm()
   if '_okcreatet' in request.POST:
       firstname = request.POST.get("firstname ")
       lastname = request.POST.get("lastname")
       adult = request.POST.get("adult ")
       b = Post(firstname='%s' % firstname, lastname='%s' % lastname)  # Внос данных в бд
       b.save()
   if '_okupdate' in request.POST:
       return HttpResponseRedirect('/tariff/')
   else:
       return render(request, "index.html", {"form": createForm)

index.html
<body>
   <form method="POST">
       {% csrf_token %}
   <table>
       {{form}}
   </table>
   <td><input name="_okcreatet" type="submit" value="OK"></td>
   <td><input name="_okupdate" onclick="location.href='{% url 'update' %}'" type="submit" value="OK"></td>
    {% block content %}

    {% endblock %}

   </form>

There are two buttons on the form. "Enter the data of a new club member" and "Edit the list of members". Before pressing the first button on the form, a static form is created to enter data about a new member. When you click on the "Edit member list" button, I want to create dynamic forms for each entry in the database (Form with a change in the last name, first name and checkbox to indicate the member about his age) (for each member of the club), through which you can change data about participant.
update.html
{% extends 'index.html' %}

{% block content %}
   <form method="POST">
       {% csrf_token %}
   <table>
       <!--{{form}}--> <!--На этом месте должны создаваться формы-->
   </table>
   <td><input name="_okclick" type="submit" value="OK"></td>

   </form>
{% endblock %}

urls.py
urlpatterns = [
   url(r'^$', views.index),
   url(r'^update/$', views.updt, name='update'),
]

Help me please. I really hope that I have clearly explained my problem as this is the first time I'm posting problems on the forum. I will be grateful to those who will help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
szelga, 2019-10-04
@szelga

1. it is better to inherit the form from ModelForm: https://djbook.ru/rel1.9/topics/forms/modelforms.html
2. to do it the way you want, I would create a list (array) of forms in the View for each records (model instance), would give them to the template, and then display them in a cycle in a separate form <form>. you also need to somehow give an id in each form, either as a hidden field or in the URL. to make it convenient to operate all of this from the point of view of the end user, it is worth screwing AJAX.
3. if you are just learning Django, it’s better to forget about your idea (edit everything from one page) for a while and write the simplest, most straightforward CRUD: separate View for the list of posts, for creating a post, for viewing and editing a post. when you work out the skills, then it will be possible to have a bunch of forms on one page, and AJAX, and everything else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question