Django
Repository: https://github.com/flayyer/integration-examples/tree/main/examples/django
Basically, we are going to use flayyer/flayyer-python to format Flayyer URL and nephila/django-meta to render them into the <head />
of your views.
#
Install- Poetry
- Pipenv
- Pip
Import Flayyer
, add meta
to INSTALLED_APPS
and setup default values:
To render the meta tags, simply add the meta
dictionary/object to the template context, and add this inside the <head>
tags in your main layout file:
#
UsageOn its most simple form: all you have to do is set an instance of Meta
from meta.views
as context['meta']
on your view.py files.
Make sure every view has a meta
value in their context, otherwise <meta />
won't be rendered.
To render Flayyer URL as og:image
and twitter:image
:
#
Function viewsThe most low-level views are just functions. Make sure to pass the context
back to the render(...)
method.
To render a Flayyer template when a page is shared can be done by overriding default values:
#
Class viewsOfficial docs
Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views.
If you don't override the context_object_name
attribute of the Views, the current object in the context is available as context['object']
for django.views.generic.DetailView
and as context['object_list']
for django.views.generic.ListView
.
Similar to the previous section, we need to set an instance of Meta
to the context['meta']
.
Another way of setting the <meta />
tags is provided by django-meta
:
If you inspect the <head />
of your HTML you should see the og:image
tags with URLs to flayyer.io
.