1

I'm trying to take input from the user from a drop-down list. I've used javascript to take input of dynamic size and then stored the input in a variable using appendChild. Now I want to send this variable back to django views where I can perform some operations on it.

I've tried using getlist and get function in django but am not able to figure out how to get the variable.

<form method = "POST" name = "myForm" onsubmit = "return validateForm()">{% csrf_token %}

    <p>Entry Condtion:</p>
    <div id ="entry-parameters"></div>
    <button type="button" onclick= "add_entry_parameter()" class="btn btn-primary m-t-15 waves-effect">Add a new Entry Condition</button> 
    <button type= "button" onclick= "remove_entry_parameter()" class= "btn btn-primary m-t-15 waves-effect">Remove Entry Condtion</button>
    <br>

    <p>Exit Condtion</p>
    <div id = "exit-parameters"></div>
    <button type="button" onclick= "add_exit_parameter()" class="btn btn-primary m-t-15 waves-effect">Add a new Exit Condition</button> 
    <button type= "button" onclick= "remove_exit_parameter()" class= "btn btn-primary m-t-15 waves-effect">Remove Exit Condtion</button>
    <br>
    <br>
    <br>    
    <input type="submit" value="submit"  >

</form>

<script>
    var list = ['open' , 'close' , 'high', 'low'];
    var addField = document.getElementById('addField');
    parameters= document.getElementById('entry-parameters');
    exit_parameters= document.getElementById('exit-parameters');

    parameters.setAttribute("name" , "parameters" );
    exit_parameters.setAttribute("name" , "exit_parameters");

    function remove_entry_parameter()
    {
        parameters.removeChild(parameters.lastElementChild);
    }
    function add_entry_parameter()
    {

            var _form = document.body.appendChild(document.createElement('form')),
            _input = _form.appendChild(document.createElement('input')),
            _condition = _form.appendChild(document.createElement('input')),
            _input2 = _form.appendChild(document.createElement('input')),
            _datalist = _form.appendChild(document.createElement('datalist'));
            _cond_list = _form.appendChild(document.createElement('datalist'));


            _input.placeholder = "First Parameter";
            _input.list = 'exampleList';
            _input.setAttribute('list','exampleList')
            _input.datalist_id = 'exampleList';

            _input2.placeholder = "Second Parameter";
            _input2.list = 'exampleList';
            _input2.setAttribute('list','exampleList')
            _input2.datalist_id = 'exampleList';

            _condition.placeholder = "Condition";
            _condition.list = 'conditionList';
            _condition.setAttribute('list','conditionList')
            _condition.datalist_id = 'conditionList';

            _datalist.id = 'exampleList';
            _cond_list.id = 'conditionList';

            var list = ['open' , 'close' , 'high' , 'low' , 'vol'];
            var cond_list = ['greater than', 'less than' , 'crossed above' , 'crossed below', 'equal to'];



            for (var i = 0; i < list.length ; i++) {
                var _option = _datalist.appendChild(document.createElement('option'));
                _option.text =list[i];   
            };
            for (var i = 0; i < cond_list.length ; i++) {
                var _option = _cond_list.appendChild(document.createElement('option'));
                _option.text = cond_list[i];   
            };

            parameters.appendChild(_form);
    }
// I've written a similar code for exit_parameters
</script>


def get_data(request):
    if request.method == 'POST':
        entry_data = request.POST.get('parameters')
        exit_data = request.POST.get('exit_parameters')
        print(entry_data)
        print(exit_data)

    return render(request, 'get_data.html' , {})

4
  • when you create the input fields through JavaScript like this do they have name="some_field_name"? within the tags? They need this for Django to pick the up Commented Jun 21, 2019 at 7:20
  • I don't give names because every input field of that type will have the same name and they won't be unique (which would be required to seperate them, I guess) I am passing them through a form so I thought using the name of the div tag would be the way. Commented Jun 21, 2019 at 7:41
  • Then you need to emulate django fromsets and give them names Commented Jun 21, 2019 at 7:47
  • So if I give form name inside the function then wouldn't the same name be applied to all the forms? I guess I forgot to mention that I'm dynamically creating form elements. Sorry, I'll edit the question Commented Jun 21, 2019 at 7:52

1 Answer 1

1

Your function add_entry_parameter starts by creating a new form on the page. You almost certainly want ONE form on the page -- no need to create new ones.

The selection made in the dropdown should be visible in the DOM (inspector) with a unique field name (it doesn't make sense for all fields on the form to have the same name, as you stated in a response above) -- all fields must have a unique name.

Once you've verified that the value in an existing, uniquely named field is updated when the user makes a selection, it should come through in the POST.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.