How to create a simple blog using Django 3 - part 11

/ #Django


It's time to do a bit of optimizations to our blog. So in this part, we're going to fix a couple of small things here and there in the code.

Optimizing the queries

A few places in the code, we get all the posts belonging to a category. This is a heavy query for the server, because the request is run multiple times. Luckily, Django has something called prefetch_related. This is a function that helps us write better queries.

When we use prefetch_related, a SQL join is used. This way, only one request to the database is sent.

The category view

Open "blog/views.py" and go down to the category view. We want to change this line:

category = Category.objects.get(slug=category_slug)

To this:

category = Category.objects.prefetch_related('posts').get(slug=category_slug)

Click here to read more about prefetch_related.

Separate the sidebar template

In each of the templates we've created, there are a code block containing all of the code for the sidebar. This code should be put into one file, and include in the templates instead.

Create a new file inside the "templates" folder in the root of the project called "sidebar.html". Copy the sidebar code into like this:

<aside class="splitter-aside">
  <div class="search">
  	<h4>Search</h4>
  	
  	<form method="post" action="/search/">
  		{% csrf_token %}
  		
  		<div class="field has-addons">
  			<div class="control">
  				<input class="input" name="query" type="text" value="{{ query }}">
  			</div>
  			<div class="control">
  				<button class="button is-info">Search</button>
  			</div>
  		</div>
  	</form>
  </div>
  
	<div class="categories">
		<h4>Categories</h4>
		
		<ul>
			{% for category in categories %}
				<li><a href="{% url 'category' category.slug %}" title="{{ category.title }}">{{ category.title }}</a></li>
			{% endfor %}
		</ul>
	</div>
</aside>

Now that we have this code in a separate file, we only have to make changes one place when we want to add something. To include this file in the templates, remove the current sidebar code and add this code there instead:

{% include 'templates/sidebar.html' %}

That's much better isn't it?

When we are including templates like this, all of the context (categories, posts, etc) will be passed down to it. Because of this, we don't need to do anything to the code.

Summary

In this part of the series you learned how to optimize QuerySets and also a couple of tips on how to keep you from repeating code. I hope you enjoyed this tutorial series. If you think there should be more parts, feel free to leave a comment below with wishes you want to include!

How to create a simple blog using Django 3 series

Part 1 - Installation
Part 2 - File structure
Part 3 - Database models
Part 4 - Admin interface
Part 5 - Creating some templates
Part 6 - Views and URLs
Part 7 - Category views
Part 8 - Media files
Part 9 - Comments
Part 10 - Search
Part 11 - Optimizations


Comments

No comments yet...

Add comment

Subscribe to my newsletter