This app allows you to add meta tags and OpenGraph properties to your HTML responses.
Installation
Install with pip:
$ pip install django-ok-seo
Update INSTALLED_APPS:
INSTALLED_APPS = [
...
'seo',
'modeltranslation', # optional
'django_jinja', # optional for jinja2 global function
...
]
Make migrations
$ python manage.py migrate
If you want to make seo models translatable, you need to install django-modeltranslation package. After that run:
$ python manage.py makemigrations
$ python manage.py migrate
to create new fields in seo models for each language.
Features
There are two options of usage:
UrlSeo - a model to fetch seo data by an url.
ViewSeo and ModelInstanceSeo - models to attach seo data for specific views (using choices) and objects (using “generic” relationships).
Available settings
SEO_USE_URL_SEO - Flag to use (display in an admin interface) only UrlSeo model. False by default.
SEO_VIEWS_CHOICES - Tuple of tuples for using with ViewSeo. The first value is the value to use in a code and a second is a verbose (translated) value.
For example:
SEO_VIEWS_CHOICES = (
('index', 'Index page'),
('faq', 'FAQ page'),
)
SEO_MODELS - List of models names to limit content type choices for ‘ModelInstanceSeo’.
For example (use lowercase):
SEO_MODELS = [
'article.article',
'auth.user'
]
SEO_DEFAULT_IMAGE - Path to default image, which will be used for ‘og:image’ property.
SEO_IMAGE_WIDTH - Value of width for image. 1200 by default.
SEO_IMAGE_HEIGHT - Value of height for image. 630 by default.
SEO_IMAGE_EXTENSIONS - List of allowed image extensions for ImageField in seo model.
SEO_IMAGE_STORAGE - Custom file storage for ImageField in seo model. ‘django.core.files.storage.FileSystemStorage’ by default.
SEO_OBJECT_IMAGE_FIELD - A name of field to get image from an object. ‘image’ by default.
By default:
['jpg', 'jpeg', 'png']
SEO_OG_TYPES - Tuple of tuples of open graph object types.
By default:
DEFAULT_OBJECT_TYPES = (
('website', pgettext_lazy('OG types', 'Website')),
('article', pgettext_lazy('OG types', 'Article'))
)
SEO_TWITTER_TYPES - Tuple of tuples of twitter card types.
By default:
DEFAULT_TWITTER_TYPES = (
('summary', pgettext_lazy('Twitter card types', 'Summary Card')),
('summary_large_image', pgettext_lazy('Twitter card types', 'Summary Card with Large Image')),
('player', pgettext_lazy('Twitter card types', 'Player')),
('app', pgettext_lazy('Twitter card types', 'App')),
)
SEO_FB_APP_ID - Common Facebook application id. Also, You can set custom id in facebook_app_id field for each seo instance.
SEO_HTML_ADMIN_WIDGET - Dictionary with default widget for top_text and bottom_text text fields in django admin interface.
For example:
SEO_HTML_ADMIN_WIDGET = {
'widget': 'TinyMCE',
'widget_path': 'tinymce.widgets'
}
SEO_DEBUG_MODE - Sets debug mode. If True adds <meta name=”robots” content=”noindex,nofollow”> to all pages.
SEO_URL_SEO_SITEMAP_PRIORITY - UrlSeo sitemap priority. 1 by default.
SEO_URL_SEO_SITEMAP_CHANGEFREQ - UrlSeo sitemap changefreq. always by default.
SEO_VIEWS_CONTEXT_NAME - name of context variable for mixins for class based views. seo by default.
Basic example to use:
Admin inline (for ModelInstanceSeo):
# admin.py
from django.contrib import admin
from seo.admin import ModelInstanceSeoInline
from apps.article.models import Article
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
inlines = [ModelInstanceSeoInline]
Views (examples for all models):
# views.py
from django.views.generic import DetailView, TemplateView
from seo.mixins.views import (
ViewSeoMixin,
ModelInstanceViewSeoMixin,
UrlSeoMixin
)
from apps.article.models import Article
class IndexView(ViewSeoMixin, TemplateView):
seo_view = 'index'
template_name = 'index.html'
class IndexViewJinja(ViewSeoMixin, TemplateView):
seo_view = 'index'
template_name = 'jinja/index.jinja'
class ArticleDetailView(ModelInstanceViewSeoMixin, DetailView):
template_name = 'article.html'
model = Article
pk_url_kwarg = 'id'
class ArticleDetailViewJinja(ModelInstanceViewSeoMixin, DetailView):
template_name = 'jinja/article.jinja'
model = Article
pk_url_kwarg = 'id'
class IndexUrlSeoView(UrlSeoMixin, TemplateView):
template_name = 'index.html'
class ArticleUrlSeoDetailView(UrlSeoMixin, DetailView):
template_name = 'article.html'
model = Article
pk_url_kwarg = 'id'
Context processor (for UrlSeo):
# ...
'seo.context_processors.seo',
Middleware (for UrlSeo) to use from request variable in tepmlates:
MIDDLEWARE = [
...
# seo
'seo.middleware.url_seo_middleware'
]
In templates:
{% load seo %}
<head>
<meta charset="UTF-8">
{% get_seo_data request.seo %}
</head>
Your templates:
*.html
{% load seo %}
<head>
<meta charset="UTF-8">
{% get_seo_data seo %}
</head>
<!-- Optional: -->
...
<h1>{{ seo.h1 }}</h1>
...
<div id='seo_text'>
{{ seo.seo_text|safe }}
</div>
*.jinja
<head>
<meta charset="UTF-8">
{{ get_jinja_seo_data(seo) }}
</head>
...
<!-- Optional: -->
...
<h1>{{ seo.h1 }}</h1>
...
<div id='seo_text'>
{{ seo.seo_text|safe }}
</div>
Inheritance
You can inherit your models from SeoTagsMixin and override necessary methods to set custom seo data for your objects.
from django.db import models
from seo.mixins.models import SeoTagsMixin
class Article(SeoTagsMixin, models.Model):
preview = models.ImageField()
short_description = models.TextField(max_length=1000)
...
def get_meta_description(self) -> str:
"""
Return meta description
"""
return self.short_description
def get_meta_image_field(self):
"""
Return image field instance to get image url
"""
return self.preview
And in a template for your DetailView, you can use:
<head>
<meta charset="UTF-8">
{% get_seo_data object %}
</head>
where object is your default context_object_name.
Also, you can use this way with ModelInstanceViewSeoMixin to still use ModelInstanceSeo, but get some data from a content object. To reach this goal, you need to override next methods:
def get_meta_title(self) -> str:
"""
Return meta title
"""
return _('{} < Some super title').format(str(self))
def get_meta_description(self) -> str:
"""
Return meta description
"""
return _(
'{} ➤ Wow! '
'✔ Amazing! '
'❖ Marvelous!'
).format(str(self))
def get_h1_title(self) -> str:
"""
Return h1 title
"""
return str(self)
If you want to get an image from the content object, you may left the image field empty in ModelInstanceSeo instance. If your image field has some specific name, you need to define a property with a name image.
Sitemap
# sitemaps.py
from seo.sitemaps import UrlSeoSitemap
...
sitemaps = {
'pages': UrlSeoSitemap
}
# urls.py
...
from django.contrib.sitemaps import views as sitemap_views
...
from somewhere.sitemaps import sitemaps
urlpatterns = [
url(r'^sitemap\.xml$', sitemap_views.index, {'sitemaps': sitemaps}, name='sitemap'),
url(r'^sitemap-(?P<section>\w+)\.xml$', sitemap_views.sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
]
Metadata
Release files for django-ok-seo 0.9.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django-ok-seo-0.9.4.tar.gz | 25.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_ok_seo-0.9.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 64.0 kB
Release files / django-ok-seo-0.9.4.tar.gz
| Download URL | django-ok-seo-0.9.4.tar.gz |
|---|---|
| Size | 25.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9111b992a52e087d52e85dc152e116c43dba3e65bb43ad838f33aebd71275988
|
|
BLAKE2b-256 checksum How to use checksums |
0d219fa7e0e10b04ce10ec7c80c4e5ce5cda848c085db524af543051ac3fb02a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.4
|
Release files / django_ok_seo-0.9.4-py3-none-any.whl
| Download URL | django_ok_seo-0.9.4-py3-none-any.whl |
|---|---|
| Size | 38.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8402d9bfc94892dfabf1d01ddf87e9a2b492da166e8c6a3e3885df0aae24b78a
|
|
BLAKE2b-256 checksum How to use checksums |
c46b1dbca395efa6571afa8efb8215754fa431890dbfdd4c93030a5c31ab8b38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.4
|
Security for django-ok-seo 0.9.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.