The blog application for django CMS providing multilingual blog functionality with advanced content management features
—
Complete set of class-based views for displaying blog content including detail views, list views, archive views, and filtered views with multilingual and SEO support.
from typing import Dict, Any, List, Optional
from django.views.generic import DetailView, ListView
from django.urls import path, URLPattern
from django.db.models import QuerySet
from django.http import HttpRequest
from djangocms_blog.models import Post, BlogCategory
from djangocms_blog.views import (
PostDetailView, PostListView, PostArchiveView,
TaggedListView, AuthorEntriesView, CategoryEntriesView,
BaseBlogView, BaseBlogListView
)
from djangocms_blog.feeds import LatestEntriesFeed
from djangocms_blog.sitemaps import BlogSitemapFoundation view classes providing common functionality for blog views.
class BaseBlogView(AppConfigMixin, ViewUrlMixin):
"""
Base view for blog functionality with app config mixin.
Attributes:
- model: Post
- queryset: Post.objects.published()
- context_object_name: 'post'
"""
def get_view_url(self) -> str:
"""Return view URL for current request."""
def get_queryset(self):
"""Return filtered queryset for current app config."""
class BaseBlogListView(BaseBlogView):
"""
Base list view for blog items.
Attributes:
- paginate_by: From get_setting('PAGINATION')
- context_object_name: 'post_list'
"""
def get_context_data(self, **kwargs) -> Dict[str, Any]:
"""Add extra context for list views."""Detail view for individual blog posts with multilingual slug support and SEO optimization.
class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView):
"""
Detail view for individual blog posts.
Features:
- Multilingual slug handling via TranslatableSlugMixin
- SEO meta tag support
- Related posts context
- Comment integration
- Social sharing data
"""
slug_field: str = 'slug'
def get_object(self, queryset=None):
"""Get post object with language handling."""
def get_context_data(self, **kwargs) -> Dict[str, Any]:
"""
Add extra context including:
- Related posts
- Categories
- Tags
- Meta information
- Previous/next posts
"""
def get_queryset(self):
"""Return published posts queryset."""Various list views for displaying filtered sets of blog posts.
class PostListView(BaseBlogListView, ListView):
"""
List view for blog posts.
Features:
- Pagination support
- Category filtering
- Tag filtering
- Date filtering
- Search functionality
"""
template_name: str = 'djangocms_blog/post_list.html'
def get_queryset(self):
"""Return filtered post queryset."""
def get_context_data(self, **kwargs) -> Dict[str, Any]:
"""Add pagination and filter context."""
class PostArchiveView(BaseBlogListView, ListView):
"""
Archive view for blog posts by date.
URL patterns support:
- /archive/YYYY/
- /archive/YYYY/MM/
- /archive/YYYY/MM/DD/
"""
template_name: str = 'djangocms_blog/post_archive.html'
date_field: str = 'date_published'
def get_queryset(self):
"""Return posts filtered by date parameters."""
def get_context_data(self, **kwargs) -> Dict[str, Any]:
"""Add date context (year, month, day)."""
class TaggedListView(BaseBlogListView, ListView):
"""
List view for posts filtered by tags.
URL: /tag/tag-slug/
"""
template_name: str = 'djangocms_blog/post_list.html'
def get_queryset(self):
"""Return posts filtered by tag slug."""
def get_context_data(self, **kwargs) -> Dict[str, Any]:
"""Add tag context."""
class AuthorEntriesView(BaseBlogListView, ListView):
"""
List view for posts by specific author.
URL: /author/author-username/
"""
template_name: str = 'djangocms_blog/post_list.html'
def get_queryset(self):
"""Return posts filtered by author."""
def get_context_data(self, **kwargs) -> Dict[str, Any]:
"""Add author context."""
class CategoryEntriesView(BaseBlogListView, ListView):
"""
List view for posts in specific category.
URL: /category/category-slug/
"""
template_name: str = 'djangocms_blog/post_list.html'
def get_queryset(self):
"""Return posts filtered by category."""
def get_context_data(self, **kwargs) -> Dict[str, Any]:
"""Add category context."""The package provides complete URL configuration for all view patterns.
# Main URL patterns (djangocms_blog.urls)
urlpatterns = [
path('', PostListView.as_view(), name='posts-latest'),
path('feed/', LatestEntriesFeed(), name='posts-latest-feed'),
path('sitemap.xml', BlogSitemap(), name='posts-sitemap'),
# Dynamic URL patterns based on permalink configuration
# Example patterns:
# path('<int:year>/<int:month>/<int:day>/<str:slug>/', PostDetailView.as_view(), name='post-detail'),
# path('<int:year>/<int:month>/<str:slug>/', PostDetailView.as_view(), name='post-detail'),
# path('<str:category>/<str:slug>/', PostDetailView.as_view(), name='post-detail'),
# path('<str:slug>/', PostDetailView.as_view(), name='post-detail'),
path('author/<str:username>/', AuthorEntriesView.as_view(), name='posts-author'),
path('category/<str:category>/', CategoryEntriesView.as_view(), name='posts-category'),
path('tag/<slug:tag>/', TaggedListView.as_view(), name='posts-tagged'),
path('archive/<int:year>/', PostArchiveView.as_view(), name='posts-archive-year'),
path('archive/<int:year>/<int:month>/', PostArchiveView.as_view(), name='posts-archive-month'),
path('archive/<int:year>/<int:month>/<int:day>/', PostArchiveView.as_view(), name='posts-archive-day'),
]# Using views in URL configuration
from django.urls import path, include
from djangocms_blog.views import (
PostDetailView, PostListView, PostArchiveView,
TaggedListView, AuthorEntriesView, CategoryEntriesView
)
# Custom URL patterns
urlpatterns = [
path('blog/', include('djangocms_blog.urls', namespace='djangocms_blog')),
# Custom view usage
path('custom-posts/', PostListView.as_view(template_name='custom/post_list.html'), name='custom-posts'),
path('custom-author/<str:username>/', AuthorEntriesView.as_view(paginate_by=5), name='custom-author'),
]
# Custom view subclassing
from djangocms_blog.views import PostDetailView
from django.shortcuts import get_object_or_404
from myapp.models import CustomModel
class CustomPostDetailView(PostDetailView):
template_name = 'custom/post_detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['custom_data'] = CustomModel.objects.all()
return context
# Accessing view context in templates
# In post_detail.html template:
# {{ post.title }}
# {{ post.abstract|safe }}
# {{ post.get_absolute_url }}
# {% for category in post.categories.all %}{{ category.name }}{% endfor %}
# {% for tag in post.tags.all %}{{ tag.name }}{% endfor %}
# Custom queryset filtering
class FeaturedPostListView(PostListView):
def get_queryset(self):
return super().get_queryset().filter(featured=True)
# Adding custom context
class EnhancedPostDetailView(PostDetailView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
post = self.get_object()
# Add related posts
context['related_posts'] = Post.objects.published().filter(
categories__in=post.categories.all()
).exclude(pk=post.pk)[:3]
# Add author's other posts
context['author_posts'] = Post.objects.published().filter(
author=post.author
).exclude(pk=post.pk)[:5]
return contextInstall with Tessl CLI
npx tessl i tessl/pypi-djangocms-blog