Hello,
I created this topic on stackoverflow and completely forgot about it. Unfortunately even after all this time there is no response, so I hope coming to the source will yield a fruitful result. I have copied the context from the original post. I can only post 2 links as I am new user but please append /questions/76501238/django-template-render-speed-issue-for-large-queryset to the stackoverflow url to view the post.
Just to note that the application is still running great and is not slow by any means, but I am just trying to be proactive incase the issue highlighted by the below does creep up in the future.
“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”
I have completed a web application for work and was doing some various tests. One of the tests was for a “exaggerated” number of items on a given page and how long it would take to load (the page is basically a dashboard for a helpdesk system) . My results are quite bad with the test page taking some 20 to 25 seconds for 1200 items on a standard spec system and 3 - 10s on a very high spec system.
I dont ever expect the active calls to reach that number on the dashboard but am looking at a worst case scenario. I have also tried with pagination, but I am probably implementing it wrong as going to the next page seems to rerun the queries and takes the same time as displaying all 1200 items irrespective of whether I am displaying 1 item or 10.
As the project is quite large and contains sensitive information, I cant really provide the code here for reproducible results but I created another basic project that basically highlights what I am experiencing. It can be downloaded here:
[Slow render - Google Drive](https://demo project)
This code is not that bad as not many attributes are referenced in the template (short.html) for loop which only refences 5 elements as opposed to the main projects 30 or so.
The view
def shorts(req):
all_shorts = Shorter.objects.all()
return render(req, 'shortner/short.html', {'all_shorts': all_shorts})
The model
class Shorter(models.Model):
short_num = models.CharField(max_length=8, primary_key=True)
short_suf = models.CharField(max_length=8)
short_ou = models.CharField(max_length=256)
short_url = models.CharField(max_length=128)
short_desc = models.CharField(max_length=512, null=True, blank=True, default='blah, blah, blah')
def __str__(self):
return self.short_ou + ' is shortened to ' + self.short_url
The html template
{% for short in all_shorts %}
<div class="accordion-item" style="border-width: thick; border-color: darkblue;">
<h2 class="accordion-header" id="heading_{{ task.reference }}">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse_{{ task.reference }}" aria-expanded="false" aria-controls="collapse_{{ task.reference }}">
<span class="row">
<span class="col"><strong>Number: </strong>{{ short.short_num }}</span>
<span class="col"><strong>Suffix: </strong>{{ short.short_suf }}</span>
<span class="col"><strong>Original URL: </strong>{{ short.short_ou }}</span>
<span class="col"><strong>Shortened URL: </strong>{{ short.short_url }}</span>
<span class="w-100"></span>
<br>
<hr>
<span class="w-100"></span>
<span class="col"><strong>Description: </strong>{{ short.short_desc }}</span>
</span>
</button>
</h2>
</div>
{% endfor %}
I am definitely suspecting this template forloop as 1 query is run in under 1ms according to the django debug toolbar yet the page takes in this example 5 seconds to fully render (using the standard/weak pc as most users have those).
The main project is currently live via IIS and is extremely fast but then again there is not much data just yet. To avoid any possible issues and assuming I am correct about the issue been with the template forloop, is there anything that I can do to try an speed up the template render?
“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”