W
W
withoutname2020-09-21 11:20:42
Python
withoutname, 2020-09-21 11:20:42

How to send a request using the POST method?

I am developing a website in django.
There is the following form:

<form action="/external/" method="POST">
    <div id="profile"></div>
        <input type="button" value="добавить" onclick="addInput()">
        <input type="button" value="создать массив" onclick="func()">
        <input type="submit" value="Выполнить">
    </form>


addInput() function.

let i = 0;
    function addInput() {
  if (i < 5) {
    i = i + 1;
    let profile = document.getElementById('profile');
    let div = document.createElement('div');
    div.innerHTML = '<label>Значение:</label> <input type="text" name="items" id="box' + (i + 1) + '">';
    profile.append(ip);
  }
        }


When you click on the "add" button, an input field appears, by condition the number of fields is limited to 5. For each field, the id increases by 1. name remains unchanged.

func() function.

function func() {
  let box = document.getElementsByName('items');
  let items = [];
 for(n=0; n < box.length; n++){
        let content = box[n].value
        items.push(content)
    }
    console.log(items)
}


Creates an array from the values ​​entered by the user in the text field of an input tag.
Everything works great, BUT! How to pass the generated array of values ​​using the POST method? The views.py file contains a function:

def external(request):
    items = request.POST.getlist('items')
    run([sys.executable, 'codetest.py', items])
    return render(request, 'main/test.html')


when you click on the "execute" button, the items variable accepts only one, the last value entered by the user, but it is necessary that it accepts the received array, which must then be accepted as an argument in the script, followed by passing the request to the database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2020-09-21
@Stalker_RED

name="items[]"
Then the entire value of items will be passed as an array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question