提问人:dinco 提问时间:5/24/2023 更新时间:5/24/2023 访问量:126
在 Django 中使用 Datatables 的服务器端处理和 SearchBuilder 时,如何在 URL 中显示参数?
How can I show the parameters in the URL when using Datatables' Server-side processing and SearchBuilder with Django?
问:
我创建了一个 Django Web 应用程序,用于查询一个非常大的数据库。为了解决这个问题,我使用带有服务器端处理的 Datatables,以及 SearchBuilder 和 Django Pagination,它们都很好用。我有一个包含数据表组件的登录页面的“索引”视图。serperate “dt-json” 视图用于使用 json 数据响应 Datatables 的 ajax 请求。我需要索引页将 ajax 请求参数作为 URL 中的端点。目前,索引页 URL 如下所示:http://127.0.0.1:8000/,我希望它看起来像这样:http://127.0.0.1:8000/?draw=1&columns%5B0%5D%5Bdata%5D=id&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&col...默认情况下,使用整个 SearchBuilder 参数字符串作为用于书签的终结点。我知道索引页没有收到 ajax 请求,但是有没有一种简单的方法可以传递要在索引 URL 中显示的数据?
这是我在 Stack Overflow 上发表的第一篇问题文章,因此我对任何差异表示歉意。 谢谢!
我的代码片段。 models.py:
class Flight(models.Model):
id = models.BigAutoField(primary_key=True)
user = models.CharField(max_length=32, blank=True, null=True)
class Meta:
managed = False
db_table = 'flight'
def get_data(self):
return {
'id': self.id,
'user': self.user,
}
urls.py:
urlpatterns = [
# homepage
path('', views.index, name='landing-page'),
# JSON object URL for datatables query.
path('dt-json/', views.dt_json, name='dt-json'),
]
索引视图:
@login_required(login_url="/login")
def index(request):
cur_user = request.user
context = {
'cur_user': cur_user,
}
return render(request, 'flight_deck/index.html', context)
dt-json 视图:
@login_required(login_url="/login")
def dt_json(request):
###
Ommitted a lot of logic here for brevity and security.
Mostly converting the request into a dict and then filtering the query into a query_set
###
# use django paginator to filter all flights into flight pages
paginator = Paginator(query_set, limit) # Show 'length' flights per page
page = offset # define the first page offest index
table_page = paginator.get_page(page) # Collect one page of flights as an object
# Datatables uses draw, recordsTotal, and recordsFiltered for processing JSON data
draw = request.GET.get('draw')
recordsTotal = model.objects.count()
recordsFiltered = query_size
data = json_data
# Generate datatables JSON object
response = {
"draw": draw,
"recordsTotal": recordsTotal,
"recordsFiltered": recordsFiltered,
"data": data,
}
return JsonResponse(response)
index.html
{% extends "./base.html" %}
{% block content %}
<h2>Welcome to the APEX Job Manager, {{ cur_user.first_name }}</h2>
<h4>Recent Flights</h4>
{% include "./components/flight_table.html" %}
{% endblock %}
flight_table.html
{% block flight_table %}
<table id="flight-table">
<thead>
<tr>
<th>ID</th>
<th>User</th>
</tr>
</thead>
</table>
<script>
var datatableColumnDefinitions = [
{data: "id"},
{data: "user"},
]
var ajaxUrl = "dt-json/"
var datatableOptions = {
searching: false,
paging: true,
lengthMenu: [5, 10, 25, 50, 100],
pageLength: 10,
serverSide: true,
processing: true,
scrollX: true,
order: [[ 0, 'desc' ]],
ajax: {
url: ajaxUrl,
type: "GET",
data: {
table: 'flight'
}
},
columns: datatableColumnDefinitions,
dom: 'Qlfrtip',
searchBuilder: {}
}
$(document).ready(function() {
var table = $('#flight-table').DataTable(datatableOptions);
// makes each table row a clickable link to the flight detail page
$('#flight-table tbody').on('click', 'tr', function () {
var data = table.row(this).data();
window.location.href="flight/"+data.id;
});
});
</script>
{% endblock %}
答: 暂无答案
评论
POST
而不是GET
,我如何处理请求正文)?如果这是你真正要问的,那就专注于这一点。如果这不是你要问的,那么我很抱歉,我错过了你问题的重点。