Giter Site home page Giter Site logo

lowerdeez / ok-seo Goto Github PK

View Code? Open in Web Editor NEW
25.0 5.0 5.0 126 KB

This app allows you to add meta tags and OpenGraph properties to your HTML responses.

License: MIT License

Python 88.44% HTML 8.38% Makefile 0.16% Jinja 3.02%
python seo seo-models meta-tags django facebook opengraph

ok-seo's Introduction

django-ok-seo PyPI version

Build status Code health Code coverage Requirements Status Python versions PyPI downloads Software license Project Status

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:

  1. UrlSeo - a model to fetch seo data by an url.
  2. 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'),
]

ok-seo's People

Contributors

lowerdeez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ok-seo's Issues

Latest django intergration

I have an error with the latest django and modeltranslation packages

Exception:

TypeError: formfield_for_dbfield() takes 2 positional arguments but 3 were given

Versions

Django: 3.0.7
Modeltranslation 0.15

P.S. my bad, wrong order in INSTALLED_APPS

seo view - not working for me

I set up the ok-seo configuration exactly how it was shown for the setting and in the view.py. I think there is some kind of disconnect from my


SEO_VIEWS_CHOICES = (
    ('home', 'Home page'),

and my

class HomeListView(ViewSeoMixin, TemplateView):
    seo_view = 'home'

Screenshot 2022-05-19 at 00 04 38

this is all that I see even though I put the title and description in the SEO view portion of the admin. Weirdly, I tried using the URL view and it worked with no problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.