微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 sortable.js 拖放 Django ManyToManyField?

如何解决如何使用 sortable.js 拖放 Django ManyToManyField?

如何将 sortable.min.js 与 Django ManyToManyField 表单保存一起使用。我的代码将 table1 拖放到 table2 工作正常,但是当我按下提交按钮时,数据未保存在数据库中。因为(文章字段)不能正常工作。我不明白如何定义 JavaScript 函数来使用 sortable.min.js 保存拖放数据。我的表单未发布数据,因为 articles 字段未获取拖放数据。请有人告诉我如何在 Django 表单中使用 sortable.min.js

models.py

class SetRundown(models.Model):
    pool_title = models.CharField(max_length=200)
    articles = models.ManyToManyField(Article)

index.html

  <form action="." method="POST">
  <input class="form-control name="pool_title" id="id_pool_title">

    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Story</th>
            </tr>
        </thead>
        <tbody id="drop" name="articles">
            {% for object in form.articles.field.queryset %}
            <tr>
                <td>{{ object.id }}</td>
                <td><a href="">{{ object.title|slice:":30" }}</a></td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Story</th>
            </tr>
        </thead>
        <tbody id="drag" name="articles">

        </tbody>
    </table>

  <button type="submit" class="btn btn-success">Submit</button>
  </form>
    <script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>

script.js

    new Sortable(drag,{
        group: '#id_articles',// set both lists to same group
    });


    new Sortable(id_articles,// set both lists to same group
        animation: 150,});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。