Unable to write Dynamic meta tag in django

Question:

Hello all i want to print dynamic meta tag values in django.
i have _header.html as partial file which is common to all the pages.
In that header file i want to add meta tags.
The header file is included in base.html and base extends all the other pages i want different meta tags for different pages. the below code is for _header.html file

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
"here i want dynamic meta tags"
<title>Random Blog</title>
<link rel="stylesheet" 
 href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="{% static 'css/main.css' %}">
</head>
Asked By: ROCKY JAMMER

||

Answers:

You can have a block meta in your main html.
Then for each template view, you can add the meta in the context (or directly inside each html)

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% block meta %}
"here i want dynamic meta tags"
{% endblock meta %}
<title>Random Blog</title>
<link rel="stylesheet" 
 href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="{% static 'css/main.css' %}">
</head>

then in a view

def some_view(request):
    my_custom_meta = 'Some custom meta'
    return render(request, 'my_template.html', {'my_custom_meta': my_custom_meta})

Then in your other template:

{% include base.html %}
{% block meta %}
{{ my_custom_meta }}
{% endblock meta %}
Answered By: Ben Boyer

you only can render a single object as meta listview or filter view dosent support I think so but here is your solution

def meta (request,pk):
     metadata=Meta.objects.get(id=pk)
     return render (request, 'html file name', context)
Answered By: Baibars 313

Here is a working example of Dynamic Meta tags.

_base.html

<title>{% block title %}Home{% endblock %}</title>
<meta name="description"        content={% block meta_desc %}       "Default Description"   {% endblock %}>
<meta property="og:url"         content={% block meta_og_url %}     "https://example.com"   {% endblock %}>
<meta property="og:type"        content={% block meta_og_type %}    "website"               {% endblock %}>
<meta property="og:title"       content={% block meta_og_title %}   "Default Title"         {% endblock %}>
<meta property="og:description" content={% block meta_og_desc %}    "Default Description"{% endblock %}>
<meta property="og:image"       content={% block meta_og_image %}   "https://example.com{% static 'images/hero.jpg' %}"{% endblock %}>

child_page.html

{% extends 'templates/_base.html' %}
{% block title %}           {{ article.title }}             {% endblock %}
{% block meta_desc %}       {{ article.summary }}               {% endblock %}
{% block meta_og_type %}    article                         {% endblock %}
{% block meta_og_title %}   {{ article.title }}             {% endblock %}
{% block meta_og_desc %}    {{ article.summary }}           {% endblock %}
{% block meta_og_image %}   {{ article.cover_image_url }}   {% endblock %}
Answered By: Aashutosh Kumar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.