【发布时间】:2017-08-31 17:34:22
【问题描述】:
我正在制作一个我希望能够拖动的模型列表,然后根据附加的排序值将订单保存为数据库中的新排序。 现在我有这个html代码:
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script src="https://code.jquery.com/ui/3.2.1/jquery-ui.js"></script>
<script src="{% static 'draggable.js' %}"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#test").submit(function(event){
$.ajax({
type:"POST",
url:"{% url 'editpage' bid=book.id pid=page.page_num %}",
data: {
'order': 1 // insert ordering here
},
success: function(){
}
});
return false;
});
});
</script>
</head>
<body>
<h1>{{page.page_title}}</h1>
<ul id="sortable">
{% for section in sections.all %}
<li style="background:blue">{{section.section_title}}</li>
{% endfor %}
</ul>
<form method='post' id ='test'>
<input type='submit' value='Test button'/>
</form>
我遇到的问题是试图让排序工作。我在哪里插入脚本(#sortable).sortable() 以便它在页面加载时工作,然后按下,POST 到视图进行解析。
如果有帮助,我的看法:
@ensure_csrf_cookie
def editpage(request, bid = -1, pid = 1):
ret = {}
b = Textbook.objects.get(id = int(bid))
page = b.pages.get(page_num = int(pid))
ret = {
'book':b,
'page':page,
'sections':page.sections,
}
if request.method == 'POST':
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + request.POST.get('order'))
return render(request,"content/editpage.html", ret)
其中很大一部分是测试代码,但我试图让它在完全实施之前小规模工作。我遇到的两个主要问题是在哪里插入可排序代码以及如何在按下按钮时将该信息保存到 POST。
【问题讨论】:
-
你不把它放在 onload 函数中,让它在页面加载时工作吗?你想发送什么到你的视图?在你看来,你想用它做什么?
-
有很多方法可以做到这一点。如果要确保它在所有其他元素之后加载,可以在页面末尾添加
<script>部分。您还可以在 JavaScript 头部使用onload、$(document).ready(function(){}),或更常用的$(function(){});。 -
另外,jQuery 有 3.2.1 版,而 jQuery UI 只到了 1.12.1 版。所以对 ui 3.2.1 的调用将失败。请查看:code.jquery.com
标签: jquery django jquery-ui django-views