How to create dynamic ids for tags in django templates

随声附和 提交于 2019-12-09 19:39:51

问题


Background: I have a dynamic table (as in I don't know its size/elements until runtime) where I am trying to populate a text area with a javascript function. To do this I plan on passing the text area's id along with the values I want to populate it with into the javascript function.

The problem is I am having trouble creating a dynamic id value for each text input field. This is how i am currently attempting to do this:

   {% with "input_"|add:applicant.id as idName %}
        <input id="{{ idName }}" type="text" value="">
        <input type="button" hidden="TRUE" onclick="">
        {{ idName }}
        <script>
            putTags({{ idName }}, {{ tags }});
        </script>
   {% endwith %}

where the function putTags() will populate the text input's contents. Unfortunately this doesn't work, as it assigns everyone's id to "input_" without appending applicant.id's value (and I have checked, applicant.id has a correct id for each iteration). Am i doing something wrong? Is there an easier way to create these unique IDs?


回答1:


You can try something like this

<input id="input_{{ applicant.id }}" type="text" value="">
<input type="button" onclick="putTags('input_{{ applicant.id }}', {{ tags }});">


来源:https://stackoverflow.com/questions/15932731/how-to-create-dynamic-ids-for-tags-in-django-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!