Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
# blog/views.py — mixins and get_context_data customisation from django.views.generic import ListView, DetailView from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib import messages from .models import Post, Tag class SuccessMessageMixin: success_message = '' def form_valid(self, form): response = super().form_valid(form) if self.success_message: messages.success(self.request, self.success_message) return response class OwnerRequiredMixin: """Restrict edit/delete to the object owner.""" def dispatch(self, request, *args, **kwargs): obj = self.get_object() if obj.author != request.user: from django.core.exceptions import PermissionDenied raise PermissionDenied return super().dispatch(request, *args, **kwargs) class PostListView(ListView): model = Post template_name = 'blog/post_list.html' context_object_name = 'posts' paginate_by = 10 def get_queryset(self): qs = Post.published.select_related('author') tag_slug = self.kwargs.get('tag_slug') if tag_slug: self.tag = Tag.objects.get(slug=tag_slug) qs = qs.filter(tags=self.tag) return qs.order_by('-published_at') def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['all_tags'] = Tag.objects.all() context['active_tag'] = getattr(self, 'tag', None) context['total_posts'] = Post.published.count() return context # blog/urls.py — as_view() usage from django.urls import path from . import views urlpatterns = [ path('', views.PostListView.as_view(), name='post-list'), path('tag/<slug:tag_slug>/', views.PostListView.as_view(), name='posts-by-tag'), ]
Result
Open