官术网_书友最值得收藏!

Creating a model mixin to take care of meta tags

If you want to optimize your site for search engines, you need to not only set the semantic markup for each page but also the appropriate meta tags. For maximum flexibility, you need to have a way to define specific meta tags for each object, which has its own page on your website. In this recipe, we will see how to create a model mixin for the fields and methods related to the meta tags.

Getting ready

As seen in the previous recipes, make sure that you have the utils package for your mixins. Open the models.py file from this package in your favorite editor.

How to do it…

Put the following content in the models.py file:

# utils/models.py
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import escape
from django.utils.safestring import mark_safe

class MetaTagsMixin(models.Model):
  """
  Abstract base class for meta tags in the <head> section
  """
  meta_keywords = models.CharField(
    _("Keywords"),
    max_length=255,
    blank=True,
    help_text=_("Separate keywords by comma."),
  )
  meta_description = models.CharField(
    _("Description"),
    max_length=255,
    blank=True,
  )
  meta_author = models.CharField(
    _("Author"),
    max_length=255,
    blank=True,
  )
  meta_copyright = models.CharField(
    _("Copyright"),
    max_length=255,
    blank=True,
  )

  class Meta:
    abstract = True

    def get_meta_keywords(self):
      tag = ""
      if self.meta_keywords:
        tag = '<meta name="keywords" content="%s" />\n' %\
          escape(self.meta_keywords)
      return mark_safe(tag)

    def get_meta_description(self):
      tag = ""
      if self.meta_description:
        tag = '<meta name="description" content="%s" />\n' %\
          escape(self.meta_description)
      return mark_safe(tag)

    def get_meta_author(self):
      tag = ""
      if self.meta_author:
        tag = '<meta name="author" content="%s" />\n' %\
          escape(self.meta_author)
      return mark_safe(tag)

    def get_meta_copyright(self):
      tag = ""
      if self.meta_copyright:
        tag = '<meta name="copyright" content="%s" />\n' %\
          escape(self.meta_copyright)
      return mark_safe(tag)

    def get_meta_tags(self):
      return mark_safe("".join((
        self.get_meta_keywords(),
        self.get_meta_description(),
        self.get_meta_author(),
        self.get_meta_copyright(),
      )))

How it works…

This mixin adds four fields to the model that extends from it: meta_keywords, meta_description, meta_author, and meta_copyright. The methods to render the meta tags in HTML are also added.

If you use this mixin in a model such as Idea, which is shown in the first recipe of this chapter, then you can put the following in the HEAD section of your detail page template to render all the meta tags:

{{ idea.get_meta_tags }}

You can also render a specific meta tag using the following line:

{{ idea.get_meta_description }}

As you may have noticed from the code snippet, the rendered meta tags are marked as safe, that is, they are not escaped and we don't need to use the safe template filter. Only the values that come from the database are escaped in order to guarantee that the final HTML is well-formed.

See also

  • The Using model mixins recipe
  • The Creating a model mixin to handle creation and modification dates recipe
  • The Creating a model mixin to handle generic relations recipe
主站蜘蛛池模板: 根河市| 桦南县| 武强县| 定州市| 巴林右旗| 桑日县| 福州市| 天峻县| 岐山县| 东阳市| 万宁市| 岳池县| 宁夏| 教育| 镇原县| 南充市| 石阡县| 张家港市| 莱阳市| 耒阳市| 玛沁县| 溧阳市| 昌图县| 离岛区| 旬邑县| 井研县| 利川市| 墨玉县| 政和县| 崇明县| 赤壁市| 广河县| 苗栗县| 紫金县| 宜良县| 长汀县| 莱州市| 襄汾县| 左云县| 分宜县| 永平县|