【发布时间】:2019-07-12 12:15:22
【问题描述】:
原文: 滚动查看最新消息,这是原帖
数据表似乎只有在直接导航到我的 Django Web 应用程序http://server.com:9001 时才有效,即使我已将所有 HTTP 流量代理到 9001。
从 http://server.com:9001/stores 查看时的屏幕截图
从 http://server.com/stores 查看时的屏幕截图
数据表只是拒绝工作。更奇怪的是,我在 /servers 有另一个数据表在做同样的事情,但在 /closed-stores 的同一个表工作始终如一(我已经刷新了几十个连续多次尝试让它断裂,但它不会)。
这些表中的每一个的 JS 都只是 $('#table-id').Datatable(); 但我会忽略它,因为它显然可以工作,所以我相信它可能是我的 Nginx.conf,或者与 Django 有什么关系?
我会注意到,在所有场景中,控制台中都有 0 个错误。
Nginx.conf
server {
listen 80 default_server;
#listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://127.0.0.1:9001;
}
location /static {
autoindex on;
alias /home/usr/applications/it-database/static/;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
值得注意的是,这些表在我的 Windows Server 2016 开发服务器上运行了近 2 个月,没有出现任何问题,但是当迁移到 CentOS 时,这种情况开始出现。我只包括这个,因为我完全不知道问题可能是什么。
更新 1: 经过一番挖掘,我发现问题在于,出于某种原因,我的 context 数据以某种方式被切断了。如果我指定端口号,我会得到所有完整的数据,因此可以将表格转换为数据表,但是当我不指定端口号时,数据将在 Store 386 处截断>(有时更多,有时更少,总是在这个区域)。
我可以看到,在某些版本的 Chrome 中,访问 /stores/ 端点会出现 net::ERR_CONTENT_LENGTH_MISMATCH 错误。很多人说,在以前的版本中,这是由于中间件订单,但后来得到了解决。
我的中间件:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
/stores/ 查看:
@login_required
def stores(request):
stores = Store.objects.exclude(street_address__contains="closed").all()
context = {
'stores':stores,
}
return render(request, 'all_stores.html', context)
商店模板:
{% extends 'base.html' %}
{% block title %} All Stores - Stores Database {% endblock %}
{% block body %}
<br>
<div class="flex_container">
<h2>Store Database</h2>
<div class="table_header" style="float: left; position: relative;">
<br>
<h4>All Locations</h4>
</div>
<table id="store-table" class="table-striped table-hover">
<thead class="thead-light">
<tr>
<th>Store #</th>
<th>Name</th>
<th>Phone</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Circuit</th>
</tr>
</thead>
<tbody>
{% for store in stores %}
<tr id="table-row">
<td><a href="/stores/{{ store.pk }}">{{ store.store_number }}</a></td>
<td><a href="/stores/{{ store.pk }}">{{ store.name }}</a></td>
<td>{{ store.phone }}</td>
<td>{{ store.city }}</td>
<td>{{ store.state }}</td>
<td>{{ store.postal }}</td>
<td>
{% for circuit in store.circuit_set.all %}
<p>{{ circuit.circuit_id }}</p>
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<script>
$(document).ready(function () {
$('#store-table').DataTable();
});
</script>
{% endblock %}
【问题讨论】:
标签: django nginx datatables django-2.0