简体   繁体   中英

How to pass a list from template( html) to view in Django?

How to pass a list from template( html) to view in Django ? I need to pass a list from template to a view using url

<div class="media-option btn-group shaded-icon">
      {% with uplist=group_list %}
          <button class="btn btn-small" id="button2">
                <a href="{% url 'upload_all_to_group' uplist %}">
                    <i class="icon-plus">Proceed</i>
                </a>
           </button>
       {% endwith %}
</div>

Here uplist a list, i need to pass it into the view function (upload_all_to_group).

Another problem is , How to specify in urls.py for request matching.

url(r'^auth_app/upload_all_to_group/(?P<uplist>(.*))/$',
views.upload_all_to_group, name="upload_all_to_group"),

I tried this method. but it's not working.

IndexError at /auth_app/auth_app/upload_all_to_group/[['user1',
'RND1', '17-11-2019', 'cc'], ['user3', 'RND2', '08-02-2018', 'ncv'],
['user1', 'group1', '02-02-2018', 'sf'], ['user23', 'RND3',
'16-12-2019', 'der'], ['user2', 'RND3', '25-06-2018', 'ddd']]/

This was the error , while pushing this method . How to handle the list in url pattern and view ?

Thanks in advance !

Your view will receive the list a string , not a list . You need to convert the string representation of the list to a list .

In your view,

uplist = self.kwargs.get("uplist")
print type(uplist)  # str

uplist_list = eval(uplist)
print type(uplist_list)  # list

Use uplist_list instead of uplist to access your list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM