发送ul列表以在Django中形成 [英] send a list of ul to form in django

查看:37
本文介绍了发送ul列表以在Django中形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模板中,我有两个连接的列表,其中一个列表最初是空的.用户必须从左侧列表中选择最多三个选项,将它们添加到右侧列表,对其进行排序(根据他/她的优先级),然后将其发送回去.

In a template I have two connected lists, one of which is initially empty. The user has to choose up to three options from the left list, add them to the right list, order them (according to his/her priorities) and then send it back.

它看起来像:

<ul id="sortable1" class="connectedSortable">
  <li value="1">Item A</li>
  <li value="2">Item B</li>
  <li value="3">Item C</li>
  <li value="4">Item D</li>
  <li value="5">Item E</li>
  <li value="6">Item F</li>
  <li value="7">Item G</li>
</ul>
</div>

<ul id="sortable2" class="connectedSortable">
 <!--here goes the initially empty list-->
</ul>

并在{%scripts%}块中:

and in {% scripts %} block:

<script>
    $( function() {
    $( "#sortable1, #sortable2" ).sortable({
      connectWith: ".connectedSortable"
    }).disableSelection();
    } );
 </script>

在models.py中,我有一个字段来存储所选信息:

in models.py I have a field to store the selected info:

preferences = CommaSeparatedIntegerField()

问题是如何将信息从更新的ul( sortable2 )发布回数据库?我可以在页面上添加一个隐藏字段 preferences ,然后使用jQuery进行填充,但这似乎是一个丑陋的解决方案.正确的是什么?

The problem is how to POST info from the updated ul (sortable2) back to the database? I can add a hidden field preferences to the page and then fill it using jQuery, but it seems like an ugly solution. What is the right one?

推荐答案

如果您想将更新的ul(sortable2)发布回数据库,则可能的解决方案之一是 ul(sortable2)应该位于表单中,您可以使用POST方法提交其数据.

If you want to POST the updated ul (sortable2) back to the database, then one of the possible solution is that the ul (sortable2) should be inside a form whose data you can submit using POST method.

现在,当任何 li元素从ul(sortable1)移至ul(sortable2)时,输入类型元素也会插入和列表元素,其中输入的值"属性必须与ul中的 li元素相同(sortable1)是.此输入元素的类型也将隐藏.

Now when any of the li element is moved from the ul (sortable1) to ul(sortable2), then an input type element is also inserted in the ul (sortable2) along with the list element, where "value" attribute of the input will have to be the same as the li element in the ul (sortable1) was. Also the type of this input element will be hidden.

现在,当您使用提交"按钮提交此表单时,您将在视图中重新获得所有数据,从而可以相应地对数据库进行更改.

So now when you'll submit this form using a submit button, you'll get all the data back in your views and hence can make changes to your database accordingly.

这篇关于发送ul列表以在Django中形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆