Skip to main content

A powerful Django SEO optimization package with modern features

Project description

Django SEO Optimizer

PyPI version Python Versions Django Versions License

A powerful Django SEO optimization package that helps you manage and optimize your website's SEO with modern Python features and best practices.

Features

  • 🚀 Modern Python (3.8+) & Django (3.2+) compatibility
  • 🎯 Smart metadata management with multiple backend support
  • 🌐 Advanced site-specific SEO optimization
  • 🔄 Intelligent redirect management with pattern matching
  • 🎨 Dynamic template support in metadata
  • ⚡ Performance-optimized with async support and caching
  • 🔍 Advanced URL pattern matching and routing
  • 🌍 Comprehensive i18n/l10n support
  • 🔒 Type hints and modern Python features
  • 📊 SEO performance analytics and reporting
  • 🤖 Automated meta tag optimization
  • 🔗 Sitemap generation and management
  • 📱 Mobile SEO optimization support

Installation

pip install django-seo-optimizer

Quick Start

  1. Add 'seo_optimizer' to your INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'seo_optimizer',
]
  1. Run migrations:
python manage.py migrate seo_optimizer
  1. Configure your SEO metadata:
from seo_optimizer import register_metadata, MetadataField

@register_metadata
class MyMetadata:
    title = MetadataField(max_length=70)
    description = MetadataField(max_length=160)
    keywords = KeywordsField()
    robots = RobotsField()
  1. Use in your templates:
{% load seo_tags %}
{% get_metadata as meta %}

<title>{{ meta.title }}</title>
<meta name="description" content="{{ meta.description }}">
<meta name="keywords" content="{{ meta.keywords }}">
<meta name="robots" content="{{ meta.robots }}">

Advanced Features

Async Support

from seo_optimizer.async_utils import AsyncMetadataManager

async def get_optimized_metadata(path):
    manager = AsyncMetadataManager()
    metadata = await manager.get_metadata(path)
    return metadata

Pattern-based Redirects

from seo_optimizer.models import RedirectPattern

RedirectPattern.objects.create(
    url_pattern="/old-blog/*",
    redirect_url="/blog/$1",
    is_regex=True,
    status_code=301
)

Performance Analytics

from seo_optimizer.analytics import SEOAnalytics

analytics = SEOAnalytics()
report = analytics.generate_report()
print(report.suggestions)

API Documentation

Core Components

MetadataField

The base field type for SEO metadata:

from seo_optimizer import MetadataField

title = MetadataField(
    max_length=70,
    required=True,
    template_support=True
)

Parameters:

  • max_length: Maximum length of the field value
  • required: Whether the field is required
  • template_support: Enable Django template syntax in field values
  • validators: List of validation functions

Specialized Fields

from seo_optimizer import KeywordsField, RobotsField, OpenGraphField

# Keywords with automatic comma separation
keywords = KeywordsField(max_keywords=10)

# Robots directives with validation
robots = RobotsField(default="index,follow")

# Open Graph metadata
og = OpenGraphField(
    title_max_length=95,
    description_max_length=200
)

AsyncMetadataManager

For high-performance async metadata operations:

from seo_optimizer import AsyncMetadataManager

async def get_meta(path):
    manager = AsyncMetadataManager()
    return await manager.get_metadata(path)

Configuration Options

Available settings in settings.py:

SEO_OPTIMIZER = {
    'CACHE_TIMEOUT': 3600,  # 1 hour
    'ASYNC_ENABLED': True,
    'MAX_ASYNC_WORKERS': 10,
    'USE_SITES_FRAMEWORK': True,
    'DEFAULT_LANGUAGE': 'en',
}

User Guide

Basic Setup

  1. Install the package:
pip install django-seo-optimizer
  1. Add to INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'seo_optimizer',
]
  1. Run migrations:
python manage.py migrate seo_optimizer

Metadata Configuration

Create a metadata configuration file (e.g., seo.py):

from seo_optimizer import register_metadata, MetadataField

@register_metadata
class BlogMetadata:
    title = MetadataField(max_length=70)
    description = MetadataField(max_length=160)
    keywords = KeywordsField()
    
    class Meta:
        use_cache = True
        use_sites = True
        use_i18n = True

Template Integration

{% load seo_tags %}

{% get_metadata as meta %}
<head>
    <title>{{ meta.title }}</title>
    <meta name="description" content="{{ meta.description }}">
    <meta name="keywords" content="{{ meta.keywords }}">
    {% if meta.og %}
        <meta property="og:title" content="{{ meta.og.title }}">
        <meta property="og:description" content="{{ meta.og.description }}">
    {% endif %}
</head>

Examples

Dynamic Metadata

@register_metadata
class ProductMetadata:
    title = MetadataField(
        template="{{product.name}} - Buy Online | {{site.name}}"
    )
    description = MetadataField(
        template="Shop {{product.name}} starting at ${{product.price}}. {{product.short_description}}"
    )

    def get_context(self, request):
        return {
            'product': Product.objects.get(slug=request.path.split('/')[-1])
        }

Multilingual SEO

@register_metadata
class I18nMetadata:
    title = MetadataField()
    description = MetadataField()

    class Meta:
        use_i18n = True
        
    def get_translations(self, language):
        return {
            'title': {
                'en': 'Welcome to our store',
                'es': 'Bienvenido a nuestra tienda'
            },
            'description': {
                'en': 'Shop the best products online',
                'es': 'Compra los mejores productos en línea'
            }
        }

Advanced URL Pattern Matching

from seo_optimizer.models import RedirectPattern

# Redirect old blog URLs to new structure
RedirectPattern.objects.create(
    url_pattern="/blog/(\d{4})/(\d{2})/(.+)",
    redirect_url="/articles/$3",
    is_regex=True,
    status_code=301
)

Best Practices

Performance Optimization

  1. Enable Caching:

    class Meta:
        use_cache = True
        cache_timeout = 3600  # 1 hour
    
  2. Use Async Support for high-traffic pages:

    async def get_product_meta(request):
        return await ProductMetadata.async_get_metadata(request.path)
    
  3. Batch Process metadata updates:

    from seo_optimizer.utils import bulk_metadata_update
    
    await bulk_metadata_update(ProductMetadata, products)
    

SEO Guidelines

  1. Title Length: Keep titles between 50-60 characters
  2. Description Length: Aim for 150-160 characters
  3. Keywords: Use 5-8 relevant keywords
  4. Mobile Optimization: Enable mobile-specific metadata
  5. URL Structure: Use clean, descriptive URLs
  6. Sitemap: Generate and update regularly

Security

  1. Sanitize User Input:

    from seo_optimizer.utils import sanitize_meta
    
    title = sanitize_meta(user_input)
    
  2. API Keys: Store sensitive data in environment variables

  3. Rate Limiting: Enable for metadata API endpoints

  4. Access Control: Restrict admin interface access

Monitoring

  1. Enable Analytics:

    SEO_OPTIMIZER = {
        'ENABLE_ANALYTICS': True,
        'ANALYTICS_RETENTION': 90  # days
    }
    
  2. Track Performance:

    from seo_optimizer.analytics import track_meta_performance
    
    performance_data = await track_meta_performance('/products/')
    
  3. Set Up Alerts for SEO issues and errors

Contributing

We love your input! We want to make contributing to Django SEO Optimizer as easy and transparent as possible, whether it's:

  • Reporting a bug
  • Discussing the current state of the code
  • Submitting a fix
  • Proposing new features
  • Becoming a maintainer

Development Process

  1. Fork the repo and create your branch from develop
  2. Install development dependencies:
    pip install -e ".[dev]"
    
  3. Make your changes and add tests
  4. Run tests and ensure they pass:
    pytest
    pytest --cov=seo_optimizer  # with coverage
    
  5. Update documentation if needed
  6. Submit a pull request

Code Style

We use several tools to maintain code quality:

# Format code
black seo_optimizer

# Sort imports
isort seo_optimizer

# Check types
mypy seo_optimizer

# Run linter
flake8 seo_optimizer

Commit Messages

Follow these guidelines:

  • Use the present tense ("Add feature" not "Added feature")
  • Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
  • Limit the first line to 72 characters
  • Reference issues and pull requests after the first line

For more detailed information about contributing, please see our Contributing Guide.

Documentation

For detailed documentation, visit avixiii.com/django-seo-optimizer

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Created and maintained by avixiii.

Support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_seo_optimizer-1.0.1.tar.gz (32.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_seo_optimizer-1.0.1-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file django_seo_optimizer-1.0.1.tar.gz.

File metadata

  • Download URL: django_seo_optimizer-1.0.1.tar.gz
  • Upload date:
  • Size: 32.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for django_seo_optimizer-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3c5bc5873d89c8550435e71d9b5384cebd939360de0d00149fed8b1e64dadefb
MD5 75d33d0713f786ee171ac2e291ed836d
BLAKE2b-256 6d1957e5c058f7c0a7c1fe8a608b8e2e82b7307229a2aa83516c13e40ca2b548

See more details on using hashes here.

File details

Details for the file django_seo_optimizer-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_seo_optimizer-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 34cbd04fe02769c09ca3abd7dedc1a9e655c2cae5c08acc31780ed27f7220c67
MD5 d9452276c57d2b0da9d8d150ab920de1
BLAKE2b-256 028efe7c7512f1d071a82c83e2ea04f872346bccce0f66a201a3fa6f4bd5cee4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page