Django:如何通过 AJAX 将 PKID 列表传递到下一个视图,然后重定向
Django: How to pass a list of PKIDs to next view through AJAX, then redirect
前提:
- 用户会看到 table 带有复选框的对象列表
在他们旁边。
- 用户可以勾选任意数量的复选框。
- 然后用户单击一个按钮,将他们重定向到另一个按钮
页面,它只处理那些 selected.
的对象
所以我需要通过嵌入在按钮中的 POST 请求将我通过 jQuery 捕获的对象 ID 数组传递到我的下一个视图(从这里的 GraphView),以显示列表只有下一个重定向页面上的那些对象。我如何在我的 URL 和我的观点中处理它?
我的jQuery:
// This is the event listener for the button click that would redirect you to the next page
$("#graphRuns").click(function() {
var array = [];
// IDs come from checkboxes. You select which objects you want
// in your next page, then click a button. Here I determine
// which IDs to add to the array through a data-attribute
// on my HTML object templates
choiceContainer.find("input:checked").each(function() {
var run_id = $(this).data("run_id");
array.push(run_id);
});
// I checked and the list of IDs is correct.
console.log(array)
// So I do a post request to the next view I want, passing the array of IDs.
$.post("{% url 'expert_import:chart_runs' %}", {
csrfmiddlewaretoken: "{{csrf_token}}",
array: array
}, function(data) {
// location.reload();
// Redirect here???
}).done();
});
我的 URLs 是我认为我做错的地方:
url(r'^runs/chart$', ChartRunsView.as_view(), name='chart_runs'),
在我的 views.py GraphView 中(单击按钮时你去的那个):
def post(self, request, *args, **kwargs):
# I can confirm the array gets to the view here.
print(request.POST)
# This is a strange bit though, the array coming in request.POST
# is named "array[]", when I never added the brackets
# in my JS var declaration, and the next line is only
# grabbing the last element in the incoming array
ids_array = (request.POST.get("array[]"))
runs = []
for run_id in ids_array:
runs.append(Run.objects.filter(pk=run_id))
return render(request, self.template_name, {'runs': runs})
我对此有几个问题。请记住,我是 Django 和一般编程的新手!
我应该将 AJAX post 指向显示列表的 URL
我 select 来自 (TableView) 的对象,或者我应该将 post 指向 URL
我要在其中使用 ID 数组的视图 (GraphView)?
我应该在 AJAX post "done" 部分做重定向吗?
功能?或者在视图中(TableView 或 (GraphView**) 在我从我的列表中获取对象列表后
DB?
我的 URL 中是否应该有一些正则表达式用于我要重定向的页面
到,处理对象 ID 列表? (注意,我不知道有多少
用户将 select)
的对象
我觉得我应该将 POST 请求连同某种标志一起发送到 TableView。然后在 TableView 的 "def post:" 上通过 Python 确定该标志是否存在并处理重定向到下一个 page/view ( GraphView),我需要 selected 对象的列表。
不确定这里的正确方法是什么。
此外,我可能使用 AJAX 使事情变得过于复杂,但我不确定如何设置 URL 以从 href 中获取列表或如何将其编码为按钮。
POST 方法通常 save data to the database。您应该使用 GET 方法来呈现模板。另外这里的AJAX也没用
您应该做的只是在 javascript 中生成一个 link 并重定向到 GraphView。 link 将包含所选 ID 的列表。这是一个生成 link:
的简单函数
function generateLink(ids_list){
var url = '"{% url 'app_name:graph_view' %}"';
url += "?id=" + ids_list.join('&id=')
return url;
}
在您的 GraphView 中,您可以使用 request.GET.getlist('id')
读取 ID
前提:
- 用户会看到 table 带有复选框的对象列表 在他们旁边。
- 用户可以勾选任意数量的复选框。
- 然后用户单击一个按钮,将他们重定向到另一个按钮 页面,它只处理那些 selected. 的对象
所以我需要通过嵌入在按钮中的 POST 请求将我通过 jQuery 捕获的对象 ID 数组传递到我的下一个视图(从这里的 GraphView),以显示列表只有下一个重定向页面上的那些对象。我如何在我的 URL 和我的观点中处理它?
我的jQuery:
// This is the event listener for the button click that would redirect you to the next page
$("#graphRuns").click(function() {
var array = [];
// IDs come from checkboxes. You select which objects you want
// in your next page, then click a button. Here I determine
// which IDs to add to the array through a data-attribute
// on my HTML object templates
choiceContainer.find("input:checked").each(function() {
var run_id = $(this).data("run_id");
array.push(run_id);
});
// I checked and the list of IDs is correct.
console.log(array)
// So I do a post request to the next view I want, passing the array of IDs.
$.post("{% url 'expert_import:chart_runs' %}", {
csrfmiddlewaretoken: "{{csrf_token}}",
array: array
}, function(data) {
// location.reload();
// Redirect here???
}).done();
});
我的 URLs 是我认为我做错的地方:
url(r'^runs/chart$', ChartRunsView.as_view(), name='chart_runs'),
在我的 views.py GraphView 中(单击按钮时你去的那个):
def post(self, request, *args, **kwargs):
# I can confirm the array gets to the view here.
print(request.POST)
# This is a strange bit though, the array coming in request.POST
# is named "array[]", when I never added the brackets
# in my JS var declaration, and the next line is only
# grabbing the last element in the incoming array
ids_array = (request.POST.get("array[]"))
runs = []
for run_id in ids_array:
runs.append(Run.objects.filter(pk=run_id))
return render(request, self.template_name, {'runs': runs})
我对此有几个问题。请记住,我是 Django 和一般编程的新手!
我应该将 AJAX post 指向显示列表的 URL 我 select 来自 (TableView) 的对象,或者我应该将 post 指向 URL 我要在其中使用 ID 数组的视图 (GraphView)?
我应该在 AJAX post "done" 部分做重定向吗? 功能?或者在视图中(TableView 或 (GraphView**) 在我从我的列表中获取对象列表后 DB?
我的 URL 中是否应该有一些正则表达式用于我要重定向的页面 到,处理对象 ID 列表? (注意,我不知道有多少 用户将 select)
的对象
我觉得我应该将 POST 请求连同某种标志一起发送到 TableView。然后在 TableView 的 "def post:" 上通过 Python 确定该标志是否存在并处理重定向到下一个 page/view ( GraphView),我需要 selected 对象的列表。
不确定这里的正确方法是什么。
此外,我可能使用 AJAX 使事情变得过于复杂,但我不确定如何设置 URL 以从 href 中获取列表或如何将其编码为按钮。
POST 方法通常 save data to the database。您应该使用 GET 方法来呈现模板。另外这里的AJAX也没用
您应该做的只是在 javascript 中生成一个 link 并重定向到 GraphView。 link 将包含所选 ID 的列表。这是一个生成 link:
的简单函数function generateLink(ids_list){
var url = '"{% url 'app_name:graph_view' %}"';
url += "?id=" + ids_list.join('&id=')
return url;
}
在您的 GraphView 中,您可以使用 request.GET.getlist('id')