Jquery 仅使用新项刷新模板中的表内容(Django)
我正在实现一个带有过滤器的表。 现有的库(如DataTables)都不适合我,因为它们基于客户端分页,我无法从数据库中获取所有数据并在客户端对其进行分页,因为它有500多万项 所以,我希望能够在输入字段中写入一些内容,并相应地过滤表中的项。 所有内容开始的URL为: 此url在custom_table.html中包含html(请参见下面的文件),其中包括名为table_rows.html的子模板,该子模板是我要刷新的子模板 为此,我做了以下工作: 我的项目结构:Jquery 仅使用新项刷新模板中的表内容(Django),jquery,python,ajax,django,django-templates,Jquery,Python,Ajax,Django,Django Templates,我正在实现一个带有过滤器的表。 现有的库(如DataTables)都不适合我,因为它们基于客户端分页,我无法从数据库中获取所有数据并在客户端对其进行分页,因为它有500多万项 所以,我希望能够在输入字段中写入一些内容,并相应地过滤表中的项。 所有内容开始的URL为: 此url在custom_table.html中包含html(请参见下面的文件),其中包括名为table_rows.html的子模板,该子模板是我要刷新的子模板 为此,我做了以下工作: 我的项目结构: project |-app
project
|-app
|-static
| |-javascript
| |-myJS.js
|
|-templates
| |-templates1
| |-custom_table.html
| |-table_rows.html
|
|-views
| |-__init__.py #Gathers all the views from othe files)
| |-ajaxCalls.py
| |-modelViews.py
|
|-urls.py
url.py
url(r'^table_rows/$', views.tableRows, name='tableRows'),
def get_filtered_data(request):
if request.method == "POST":
try:
[...]
reg_list = query response with filtered Data from DB
[...]
return JsonResponse({"status": "ok", "result":reg_list})
except Exception as e:
return JsonResponse({"status": "none"})
else:
return JsonResponse({"status": "none"})
def tableRows(request):
print("I'm in")
return render(request, 'templates1/table_rows.html', {
})
自定义表格.html
#extend and loads here
{% block content %}
<table id="myTable"">
...
<thead> #headers and filters for each column </thead>
<tbody id="table_body">
{% include "templates1/table_rows.html" %}
</tbody>
</table>
{% endblock %}
ajaxCalls.py
url(r'^table_rows/$', views.tableRows, name='tableRows'),
def get_filtered_data(request):
if request.method == "POST":
try:
[...]
reg_list = query response with filtered Data from DB
[...]
return JsonResponse({"status": "ok", "result":reg_list})
except Exception as e:
return JsonResponse({"status": "none"})
else:
return JsonResponse({"status": "none"})
def tableRows(request):
print("I'm in")
return render(request, 'templates1/table_rows.html', {
})
modelViews.py
url(r'^table_rows/$', views.tableRows, name='tableRows'),
def get_filtered_data(request):
if request.method == "POST":
try:
[...]
reg_list = query response with filtered Data from DB
[...]
return JsonResponse({"status": "ok", "result":reg_list})
except Exception as e:
return JsonResponse({"status": "none"})
else:
return JsonResponse({"status": "none"})
def tableRows(request):
print("I'm in")
return render(request, 'templates1/table_rows.html', {
})
一切正常,直到我必须将注册表列表加载到表中为止。
Chrome控制台中出现以下错误:
jquery-3.1.1.min.js:4 POST 404(未找到)
因此,很明显,url是乱七八糟的,因为django在现有视图的基础上编写新视图。也许路线有问题?
我不知道如何继续,请帮助。找不到404是因为
$("#table_body").html('').load(
"app/views/tableRows.html",
{reg_list: json.result}
);
需要作为静态资源而不是视图模板进行访问。将tableRows.html
复制/移动到app/static/
并将您的myJS.js
更新到正确的静态url
或者在
url.py
中定义与app/views/tableRows.html
匹配的视图。看起来好像只共享了url.py
的一部分,所以我遗漏了部分内容。DataTables.net有一个服务器端选项,其中分页和筛选都是在服务器上完成的。看一看,我已经试过了,但还是没能成功。不过,谢谢你的提示。