E
E
Eldybyhtyn2021-11-04 12:00:25
Django
Eldybyhtyn, 2021-11-04 12:00:25

How to transfer data from dynamic inputs to database?

Django project. There is a certain table which data is deduced from the SQLite database.
6183a03f0d563610240599.jpeg
The table has a viewing and editing mode
6183a0510a723827892106.jpeg
Using inputs, you can edit the table and add new columns and rows. They are added using js.
After pressing the "send" button, the data from the inputs should be sent to the database. How can I make django go through the entire page and add the data from the inputs to the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-04
@Eldybyhtyn

to make django look at the whole page

Well, first I want to make sure: do you understand the difference between the code on the server and the code on the client?
In case it's not

Цепочка обычно такая:
1. Клиент посылает запрос, в том числе при переходе по ссылке
2. Сервер генерирует страницу (тут работает питон), которая может включать в себя JS код.
3. Сервер отправляет страницу клиенту. Начиная с этого момента Питон не может ничего сделать.
4. Клиент разбирает страницу и выполняет JS код.
5. JS код может выполнить еще один запрос к серверу.

So not "let django view the page", but "the JS code on the page should form a background (AJAX) request to a separate URL, in the body of which pass the contents of the table to the server. The handler for this URL on the server side must accept the request body, validate the received data and put them in a table.
Then the task is divided into several typical subtasks:
1. Sending an AJAX request to JS by pressing a button on the page (googled).
2. Sending an AJAX request with a body (google it, see the JS documentation).
3. Accepting the request body in Django (google it, see the Django documentation).
4. Turning the request body into a data structure that is convenient for you (you must do). You can pass JSON, it's easy to form with JS and easy to read with Python. For example, a list of objects, where each object is one row of the table, and the fields of the object are the values ​​of the columns.
5. Enumeration of elements in the structure and their validation. You do it, because only you know what data is being transferred.
6. For valid data - adding to the database table (google it, see sqlite3 module documentation).
If you have questions about a specific subtask, ask them separately.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question