A Basic SEO for Django

First of all, you might be asking, what is SEO? Well, "Search Engine Optimization" is about making small modifications to parts of your website that will make it easier for search engines to crawl, index and understand your content.
Here, I'm going to show a quick guide on how to implement the SEO basics in your Django project.

Meta tags

Meta tags are a way to provide search engines with information about your site. These tags must be placed within the  tag in the HTML document.


<meta name="description" content="A good summary about your page.">

This meta tag is the most important, it provides a short description about each page of your website. Search engines, such as Google, will often use them as snnipets of your pages.

Good Pratices:
  • Write unique description for each page
  • One to two sentences or one short paragraph
  • Write a good summary of what your page is about
Bad Pratices:
  • Use generic descriptions
  • Fill the description with only keywords
  • Use the same description across all pages of your website


<title>Django REST Framework 3.1 -- Classy DRF</title>

When your page appears in a search result, this tag will usually be in the first line.

Good Pratices:
  • Brief and descriptive title
  • Unique title tag for each page
Bad Pratices:
  • Using a single title to all pages of your website
  • Use generic titles

How to do it with Django?

Well, there are many ways to include meta tags in your django project.

  • The simplest way is to add them directly in your template's pages, but I strongly do not recommend doing this for obvious reasons.
  • Another way is to pass the meta tag of each view to their respective view through the context_data.
  • If you have a really big project, with hundreds or thousands pages, you can also try the Django-SEO Framework, but from the date this post was written, it was not ready for Django 1.7.

Don't waste your time thinking about keywords meta tags, major search engines don't use it anymore.


It's a XML file where you can list the pages of website to tell search engines about your website organization. Search engine web crawlers read this file to more intelligently crawl your site. It look like this:

<urlset xmlns="">

How to do it with Django?

Django comes with a sitemap framework that makes this task pretty easy.

  • Add django.contrib.sitemaps in your installed apps.
    # ...
  • Make sure your TEMPLATES setting contains a DjangoTemplates backend and APP_DIRS options is set to True.
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    # ...
  • Create a file with your sitemap class:
class MySiteSitemap(Sitemap):
    changfreq = 'always'

    def items(self):
        return Question.objects.all()

    def lastmod(self, item):
        last_answer = Answer.objects.filter(question=item)
        if last_answer:
            return sorted(last_answer)[-1].date

items is a method that should return an object list of your sitemap.

lastmod can be a method or an attribute, when it's a method it should take an object returned by items() and return this object's last-modified date, as Python datetime.datetime object.

location can be a method or attribute, when a method it should return the absolute path given a object returned by items(). When it's not specified the framework will call get_absolute_url() on each object returned by items().

See others attributes and methods at Django Sitemap Framework documentation.

  • Set your
from django.contrib.sitemaps.views import sitemap
from entry.sitemaps import EntrySitemap
urlpatterns = patterns('',
    # ...
    # SEO
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': {'entry': EntrySitemap}},

So, these are simple tips that will improve your search engine performance, you can learn other techniques in these links below:

Victor Carriço

Fullstack Developer