Last active
July 4, 2020 05:20
-
-
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]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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