Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Class-based Views Customizing Class-based Views LoginRequiredMixin

Great, now that you have it available, add the LoginRequiredMixin to the create, update, and delete views.

Great, now that you have it available, add the LoginRequiredMixin to the create, update, and delete views.

I added LoginRequiredMixin to create, update, and delete. I feel like it's this simple but I guess I'm missing something.

articles/views.py
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin

from . import models


class ArticleList(generic.ListView):
    model = models.Article


class ArticleDetail(generic.DeleteView, generic.DetailView):
    model = models.Article
    template_name = 'articles/article_detail.html'


class ArticleCreate(generic.CreateView, LoginRequiredMixin):
    fields = ('title', 'body', 'author', 'published')
    model = models.Article


class ArticleUpdate(generic.UpdateView, LoginRequiredMixin):
    fields = ('title', 'body', 'author', 'published')
    model = models.Article


class ArticleDelete(generic.DeleteView, LoginRequiredMixin):
    model = models.Article


class ArticleSearch(generic.ListView):
    model = models.Article

    def get_queryset(self):
        qs = super().get_queryset()
        term = self.kwargs.get('term')
        if term:
            return qs.filter(body__icontains=term)
        return qs.none()

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

The order of the Mixins is important. LoginRequiredMixin must be leftmost in the inheritance chain.

"When using class-based views, you can achieve the same behavior as with login_required by using the LoginRequiredMixin. This mixin should be at the leftmost position in the inheritance list."

https://docs.djangoproject.com/en/2.2/topics/auth/default/

Good luck with Django! You can build some great sites with the framework.

JM