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

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
主站蜘蛛池模板: 鸡泽县| 黄龙县| 弥渡县| 阿克苏市| 宜君县| 兴业县| 平湖市| 徐水县| 雷波县| 金湖县| 高陵县| 特克斯县| 梧州市| 城市| 南岸区| 黄大仙区| 肥乡县| 沈阳市| 南城县| 彭阳县| 宁津县| 乐业县| 浠水县| 宜君县| 清水河县| 垦利县| 日照市| 龙门县| 涪陵区| 彭阳县| 蓝田县| 化州市| 双桥区| 福鼎市| 长乐市| 大石桥市| 大洼县| 会宁县| 中卫市| 铜梁县| 游戏|