Home > Mobile >  how to set dynamic seo tags in in django template
how to set dynamic seo tags in in django template

Time:11-02

I'm working on a djanog project and i want to render dynamic meta tags and titles for every page in it. for now i'm trying to do it like this i have added block in header.html file like this

{% block seo %}
{% endblock %}

hierarchy of main template (from which all other templates are extending)

{% include 'header.html' %}
{% include 'menu.html' %}
{% block body %}
{% endblock %}
{% include 'footer.html' %}

now on app templates i'm trying to render those seo tags like this

{% extends 'main.html' %}
{% block seo %}
<title>example.cpm</title>
<mete name="description" content="lorem ipsum">
{% endblock %}

but this approach is not working for me, please help me in this regard

CodePudding user response:

You should define this to the header.html :

{% block seo %}
    <title>example.cpm</title>
    <meta name="description" content="lorem ipsum">
{% endblock %}

And there is no need to redefine the block seo on the app template.

According to the documentation, The include tag should be considered as an implementation of “render this subtemplate and include the HTML”, not as “parse this subtemplate and include its contents as if it were part of the parent”. This means that there is no shared state between included templates – each include is a completely independent rendering process.

Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered - not blocks that can be overridden by, for example, an extending template.

CodePudding user response:

The Django template engine does not support this. You have to place the seo block placeholder in your main template, otherwise it won't be rendered when extending.

{% block seo %}
{% endblock %}
{% include 'menu.html' %}
{% block body %}
{% endblock %}
{% include 'footer.html' %}
  • Related