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 corresponding get_*() methods, used to render the associated meta tags, are also added. Each of these passes the name and appropriate field content to the core get_meta_field() method, which uses this input to return rendered markup based on the meta_field.html template. Finally, a shortcut get_meta_tags() method is provided to generate the combined markup for all of the available metadata at once.

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

{% block meta_tags %}
{{ block.super }}
{{ idea.get_meta_tags }}
{% endblock %}

Here, a meta_tags block has been defined in a parent template, and this snippet shows how the child template redefines the block, including the content from the parent first as block.super, and extending it with our additional tags from the idea object. You could also render only a specific meta tag by using something like the following: {{ idea.get_meta_description }}.

As you may have noticed from the models.py code, 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. The database data in meta_keywords and other fields will automatically be escaped when we call render_to_string() for the meta_field.html template, because that template does not specify {% autoescape off %} in its content.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.122.4