How can I load values into the form from the desired table row by clicking?
Good day! I plan to build a mini application. Which consists of one page (the main one) - a template. And also one secondary page - on the second page I fill in information into the model table through a form. On the other page (the main one) - I display information in the form of a table. I plan to place the form next to the table. The most difficult thing is that in this form I would like to receive data and write/send data from a specific table row.
For example, I click on an element in a table row and information appears in the form - the contents of this row that I clicked or pressed.
Information appears loaded in the form, values from the table row - which can then be edited and the data can be written back into the table model.
Can you think of something about this? I would be very grateful for any tips and help.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>title</title>
</head>
<body>
<div>
<h4>Мероприятия</h4>
</div>
<br/>
<div>
<form method="GET" action="{% url 'table' %}">
{{ form.as_p }}
<input type="submit" value="Submit" class="button" />
</form>
</div>
<br/>
<div>
<table style="font-family: Arial, Helvetica, sans-serif; font-size: small;">
<thead>
<tr>
{% for col in table.columns %}
<th>
{{ col }}
</th>
{% endfor %}
</tr>
</thead>
{% for index, row in table.iterrows %}
<tr>
{% for cell in row %}
<td style="text-align: center;">
{{ cell }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
<br/>
<hr/>
<div>
<ul>
{% for book in books %}
<li>
<a href="{% url 'book-detail' book.pk %}">{{ book.nameobject }}</a>
</li>
{% endfor %}
</ul>
</div>
<br/>
<hr/>
<div>
<table>
<thead>
<tr>
<th>ОМСУ</th>
<th>Наименование объекта</th>
<th>Тип объекта</th>
<th>Объём финансирования</th>
<th>Подрядчик</th>
</tr>
</thead>
<tbody>
{% for book in books %}
<tr>
<td>{{ book.city }}</td>
<td >
<a href="{% url 'book-detail' book.pk %}">{{ book.nameobject }}</a>
</td>
<td>{{ book.typeobject }}</td>
<td>{{ book.budget }}</td>
<td>{{ book.podryadchik }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
--
def table(request):
context = {}
book_filter = BookFilter(request.GET, queryset=ArkiObject_1.objects.all())
table_2 = book_filter.qs
context['form'] = book_filter.form
table = pd.DataFrame.from_records(table_2.values("city", "nameobject", "typeobject", "budget", "podryadchik"))
context['table'] = table
context['books'] = table_2
return render(request, "table.html", context)
def book(request, pk):
context = {}
form_2 = Form_GPR(request.POST or None)
if form_2.is_valid():
model_instance = form_2.save(commit=False)
value_work = model_instance.work
value_peoples = model_instance.peoples
value_pk = pk
new_entry = ArkiGpr(name=value_pk, work=value_work, peoples=value_peoples)
new_entry.save()
return redirect("book-detail", pk)
context['form_2'] = form_2
""" Gets an individual book object from the database, based on its ID/PK. Renders a detail template """
book = get_object_or_404(ArkiObject_1, pk=pk)
context['book'] = book
filter_qs = ArkiGpr.objects.filter(name=pk)
filter_qs = filter_qs.values("work", "peoples")
context['books'] = filter_qs
return render(request, 'bookdetail.html', context)