[Solved] python django only the first statement statement can be accessed


The problem is your function searched() in the script-tag.

If you have for example following name-instances:

[
    {
        'First': 'foo',
        'Last': 'bar',
    },
    {
        'First': 'foobar',
        'Last': 'barfoo',
    }
]

So your rendered ifelse in the function would look like this:

function searched(){
    nameSearched = document.getElementById('name').value;
    if ("foo" == nameSearched) {
        ...
    } else {
        ...
    }
    if ("bar" == nameSearched) {
        ...
    } else {
        ...
    }
}

So as you can see, you are always running into the else if you entered foo.

What you could do is the following in your for-loop:

 <script type="text/javascript">
    function searched(){
      nameSearched = document.getElementById('name').value;
      {% for names in name %}
        {% if forloop.first %}
          if ("{{ names.First }}" == nameSearched) {
            document.getElementById('dis').innerHTML = "{{ names.Last }}";
          }
        {% else %}
          else if ("{{ names.First }}" == nameSearched) {
            document.getElementById('dis').innerHTML = "{{ names.Last }}";
          }
        {% endif %}
      {% endfor %}
      {% if name %}
        else {
          document.getElementById('dis').innerHTML = "none";
        }
      {% endif %}
    }
</script>

This would result in the following:

function searched(){
    nameSearched = document.getElementById('name').value;
    if ("foo" == nameSearched) {
        ...
    }
    else if ("foobar" == nameSearched) {
        ...
    }
    else {
        ...
    }
}

0

solved python django only the first statement statement can be accessed