Stockton SEO Agency Company Articles Call 801-360-8331
Django SEO Optimization

Django SEO Optimization

Part of our special sauce here at the Stockton SEO Agency is Django. We build, develop, update, and fix Django sites and apps. If you are looking for a helping hand, reach out to us!

Keypoints

General Application

A Django website may or may not even have a frontend to optimize for SEO. However, I like to write Django sites with their own frontend, using Views. But I have built Django sites that are just an API that requires a separate frontend that pulls data from the API. So, how does someone optimize these sites for SEO?

Well, I wish I could just provide a Django documentation page, but if you search for "SEO" on the Django Project website, you don't find any results:

0 results for SEO in version 5.1

So, it's up to us to talk about optimizing a Django site for SEO! First off, some important things, whether you are using Views/Templates, or building an API:

Loading Speed

You'll want to ensure that your Django site responds quickly. So you may need to optimize your database queries, in some situations. The django ORM works great, but if you have a ton of data, then optimization may be needed, using prefetch_related or select_related. There are other techniques on that doc page as well, but those are good places to start.

Only Deliver Necessary Data

Delivering more data than needed could slow things down, or give data that you don't showing up. Especially when dealing with an API, you don't want to serialize too much, or too little. When you have a lot of related models, you might find that providing all of the relations with all of their fields may be overkill.

Optimizing Views

A Django view is "is a Python function that takes a web request and returns a web response. This response can be the HTML contents of a web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really" (Django Project). Most of the time, in my case, the view returns a web page. The view handles the logic for the dynamic data generation, and then produces the page.

So in this case, you just need to ensure that you cover the basics of SEO for each of these pages. Such as:

Writing Django views allows you to develop a site in almost-pure HTML. You just have some loops and conditionals and things in the template to help generate dynamic content.

The tricky thing here, is that you aren't using something like WordPress. Django puts you more in the driver seat of what data ends up where, and why. That's part of why I like using the framework, as it's exceptionally customizable. That's not to say that WordPress or another platform isn't, but the way that Django flows because of it's Python language underneath, that is what keeps me using it.

The Template Trick

I want to mention something I call the "template trick." It's not a trick at all, and really it's basic usage of the template system.

Whenever I have a database model that represents a page or blog post, I do two things:

This allows me to write all my SEO important stuff in the Django admin, but then get all the results in the resulting page.

A "Post" model may look like this:

class Post:
    title = models.CharField(max_length=80)
    meta_description = models.CharField(max_length=150)
    social_image = models.ImageField()

So this fills out the header-title tag, the header-meta-description tag, and provides an OpenGraph description and image for social media sharing. Usually my post/page template would begin with this:

{% extends 'layout.html' %}

{% block title %}{{ post.title }}{% endblock %}
{% block meta %}
   <meta name="description" content="{{ post.meta_description }}">
   <meta property="og:title" content="{{ post.title }}">
   <meta property="og:description" content="{{ post.meta_description }}">
   <meta property="og:image" content="{{ post.social_image.url }}">
{% endblock %}

Depending on how you do this, you may need to prefix the og:image with your domain and https://. But this is a good place to start.

Optimizing an API

So an API could be more tricky to optimize for SEO. Even when searching the popular "Django REST Framework" website, a search for SEO yields nothing, again:

No results found

However, one strategy for optimizing SEO for an API, is to optimize the documentation for the API. So if you are building a public facing API, then that may be something you consider.

But, if you are building an API that simply provides a backend to a "Single Page Application" (SPA), or some other frontend library, then that's a different story. This is because the content from the API is delivered after* the client/browser/crawler loads the page. So the client gets all the Javascript, and they get the info about how to get the content, but then it is up to the client device to pull that content. Fortunately, I think I have heard that Google does accounts for some of this client responsibility to pull the content from the API, but I am unsure about how much the Google will actually fetch.

If you are in a situation where you have a frontend that needs to talk to the backend in order to provide content, then you might consider an intermediary server that pulls the content from the API, and merges your frontend code with the content before sending it out to the client. Each frontend framework usually has a complimentary program to do this.

Wrapping Up

Well, the principles and methods for SEO optimization in Django aren't really different from other website tools. But Django does gives you a behind-the-scenes look at what is going on, and you have access to almost every aspect to a site.

If you are using Django views, then one thing I would recommend is a basic and useful package called django-request. That is something you can add to the INSTALLED_APPS that keeps track of the requests and provides a simple graph. You can find that here:

https://pypi.org/project/django-request/

Valete!

Written by Jon

Author Profile Picture

Hi, I'm Jon. My family and I live in Utah, where we enjoy sports and raising our chickens. I have a bachelors degree in Software Development, various computer & project management certifications, and I've worked for web hosting and other dev/online companies for over a decade.