【发布时间】:2020-12-01 22:38:21
【问题描述】:
我在 Django 中构建了应用程序并将其上传到 Heruko。 我正在使用亚马逊的 SQLPostgres。
并且我已经安装了 Newrelic 插件和 Django 调试工具。
我的问题是在我的应用中加载页面很慢: 我将向您展示我加载页面非常慢的部分代码。 你能指出我在查询中做错了什么吗?
新遗物:
Django 工具错误:
Views.py
@login_required(login_url='login')
def students(request):
form = StudentForm()
form.fields['desc'].widget.attrs['class'] = "form-control"
form.fields['firstname'].widget.attrs['class'] = "form-control"
form.fields['lastname'].widget.attrs['class'] = "form-control"
form.fields['sid'].widget.attrs['class'] = "form-control"
form.fields['studentclass'].widget.attrs['class'] = "form-control"
form.fields['email'].widget.attrs['class'] = "form-control"
form.fields['phone'].widget.attrs['class'] = "form-control"
form.fields['main'].widget.attrs['class'] = "form-control"
form.fields['submain1'].widget.attrs['class'] = "form-control"
students = Student.objects.all()
if request.method == "POST":
form = StudentForm(request.POST)
if form.is_valid():
form.save()
return redirect('students')
return render(request, "studentform/students.html", {"form": form, "students": students})
模板.html
{% extends 'studentform/controlpanel.html' %}
{% load static %}
{% block content %}
<div class="container">
<h1> תלמידים<a style="float: left; padding-left: 20px" href="{% url 'export_csv'%}"><img style="height: 35px;width: 35px" src="https://d1yjjnpx0p53s8.cloudfront.net/styles/logo-thumbnail/s3/072015/excel_0_0.png?itok=tetyiA_8"></a></h1>
<div class="card shadow p-3 mb-5 bg-white rounded">
<div class="card-body ">
<form id="students-form" method="POST">
<div class="row">
{% csrf_token %}
{% for field in form %}
{% if forloop.counter|divisibleby:"3" %}
</div>
<div style="padding-top: 10px" class="row">
{% endif %}
<div class=" col-md-4 col-sm-12 ">
<label >{{ field.label }}</label>
{{ field }}
</div>
{% endfor %}
</div>
<div style="padding-top: 10px" class="row justify-content-center text-center">
<input type="submit" class="btn btn-primary" value="צור תלמיד" />
</div>
</form>
<hr />
<div class="container">
<table style="width:100%" class="display nowrap" id="my_students">
<thead>
<tr>
<th>שם</th>
<th>שם משפחה</th>
<th>ת.ז</th>
<th>איימיל</th>
<th>טלפון</th>
<th>כיתה</th>
<th>מגמה ראשית</th>
<th>חוגים</th>
<th> עדכון</th>
<th>מחיקה</th>
</tr>
</thead>
<tbody>
{% for i in students %}
<tr>
<td>{{i.firstname}}</td>
<td>{{i.lastname}}</td>
<td>{{i.sid}}</td>
<td>{{i.email}}</td>
<td>{{i.phone}}</td>
<td>{{i.studentclass.name}}</td>
<td>{{i.main.name}}</td>
<td> {% for n in i.hog.all %}
{{n.name}}
{% endfor %}
</td>
<td><a class="btn btn-sm btn-info" href="{% url 'updatestudent' i.id %}">עדכון</a></td>
<!-- <td><a class="btn btn-sm btn-danger" href="{% url 'deletestudent' i.id %}">מחיקה</a></td> -->
<td><button value="{{i.id}}" class="btn btn-sm btn-danger deletestu" href="#">מחיקה</button></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock content %}
更新:
我使用 prefetch_releted,它似乎修复了 230 个查询。
但现在我有: 335.75 毫秒(15 个查询,包括 8 个相似和 8 个重复)
ms 没有变化,但查询的数量发生了变化。
【问题讨论】:
-
@Alasdair 其付费网站。你能告诉我如何在我的代码上实现它吗?
-
看看
select_related和prefetch_related,例如students = Student.objects.all().select_related(). -
@Alasdair 它仍然是相同的次数和相同数量的查询它没有多大帮助。
-
这些是你减少查询次数所需要的工具,恐怕我不能说得更详细。我会尝试添加/删除视图的某些部分以查看导致附加查询的原因,然后您可以专注于这些部分。当您没有展示模型时,其他人将很难提供帮助。
标签: python django postgresql django-views django-templates