Django DeleteView with AJAX

The default Django generic DeleteView is not perfectly adapted for AJAX requests. A much simpler AjaxDeleteView can easily be implemented using the same mixins as Django’s generic class-based views: class AjaxDeleteView(SingleObjectMixin, View): “”” Works like DeleteView, but without confirmation screens or a success_url. “”” def post(self, *args, **kwargs): self.object = self.get_object() self.object.delete() return HttpResponse(status=204) This […]

Return a user’s own objects with Django’s class-based views

There is a very simple way to only return the connected user’s own objects with Django’s generic class-based views. When you extend get_queryset() on any view that implements SingleObjectMixin or MultipleObjectMixin (almost all of them), you can filter the default QuerySet to match your needs. It becomes fairly easy to create a mixin that filters […]

Pagination with Django’s generic ListView

Did you know the Django ListView supports pagination out of the box? All you need to do is specify the number of items per page with the paginate_by attribute: class ArticleList(ListView): model = Article paginate_by = 10 The queryset available in object_list will be paginated, so you will only get 10 results. page_obj and paginator […]