Skip to content

Instantly share code, notes, and snippets.

@somangshu
Last active July 4, 2020 05:20
Show Gist options
  • Save somangshu/05b5502e3230faae0181a9a7e59ebc94 to your computer and use it in GitHub Desktop.
Save somangshu/05b5502e3230faae0181a9a7e59ebc94 to your computer and use it in GitHub Desktop.
Setting up meta tags in django base template for use in react[client side]
// fetch the key from the base html using the below script, this
// can be used anywhere in your javascript, but consider storing it
// in local cache for better usage
const clientMeta = document.querySelector('meta[name="client-key"]');
const clientKey = vapidMeta.content;
{% load static %}
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>{% block title %}{{ title }}{% endblock %}</title>
// we load the client key sent by the backend into the meta tag when the root page loads
<meta name="client-key" content="{{ client_key }}">
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="{% static 'public/js/app.js' %}"></script>
</body>
</html>
from django.shortcuts import (render,)
from django.conf import settings
def home_view(request):
# from you django config we get the key required which is loading though the env
client_key = webpush_settings.get('CLIENT_KEY')
context = {'title': 'Home', 'client_key': client_key}
return render(request, 'base.html', context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment