Adding rel="nofollow" and target="_blank" attributes to links in django project

created at 03-11-2021 views: 58

After googling for a while for a plugin to automatically  add  rel="nofollow" and target="_blank" attributes to all html a tags for my Django blog, nothing was found but this one called md_nofollow by bhch

Unfortunately, this plugin doesn't work for my blog, maybe its because of the version of python markdown. So I decided  to write my own one.


python markdown 3.x.x, see here for installation

since the APIs are different with versions of python markdown, so this is important.


the name of this plugin is python_markdown_nofollow, you can easily install this plugin by command:

pip install python-markdown-nofollow


here is a detailed example of how to use this plugin for your django blog.

template tags

in the frontend, we use a template tag to translate markdown syntax to html like:

{{ blog.content|std_markdown|safe }}

in my app directory, I create a directory called templatetags:

    ├── app
    │   ├── templatetags
    │       ├──

and under templatetags directory we create a file called

in the file we are going to do the translation from markdown to html:

from django import template
from django.template.defaultfilters import stringfilter

import markdown # import python markdown

register = template.Library()

def std_markdown(value):
    return markdown.markdown(value, extensions=[

after doing this, our front will work well with a link looks like (use the inspect of a browers):

<a href=''>external link</a>

Use python_markdown_nofollow

now we integrate python_markdown_nofollow to our translator by simply adding:

def std_markdown(value):
    return markdown.markdown(value, extensions=[

restart our project, in our front end we will see:

<a href='' rel="external nofollow" target="_blank">external link</a>

Why use python_markdown_nofollow

well, if you have learnt something about SEO, you may know it is good to add rel="nofollow" attribute for external links in our websites, which would be good for the SEO of our site.

Since the targets of this plugin are all the <a> tags in our content, which may include links of our own site, so external nofollow rather than nofollow was used.

sources of python_markdown_nofollow

this plugin was released on:

created at:03-11-2021
edited at: 03-18-2021: