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 Custom Mixin

Chris Komaroff
PLUS
Chris Komaroff
Courses Plus Student 14,198 Points

Mixins video, Challenge Task 2 of 4, trying to return an attribute from the instance

Question (posted with link to challenge question):

Challenge Task 2 of 4

Now add a get_success_message method to your mixin that returns the success_message attribute from the instance.

articles/mixins.py

from django.contrib import messages

class SuccessMessageMixin:
    # Added this attribute in task '1 of 4'
    success_message = ''

    # Added this method in task '2 of 4'
    def get_success_message(self):
        return self.success_message

Am I misreading the question? He says "from the instance". Is that a clue? I thought he refers to an instance of SuccessMessageMixin, but maybe he refers to a model instance. Maybe I am just making a silly mistake (sorry), or else he is sending me off to study more about mixins. Thanks for any info!

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

from . import mixins
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(LoginRequiredMixin, generic.CreateView):
    fields = ('title', 'body', 'author', 'published')
    model = models.Article


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


class ArticleDelete(LoginRequiredMixin, generic.DeleteView):
    model = models.Article
    success_url = reverse_lazy('articles:list')


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()
articles/mixins.py
from django.contrib import messages

class SuccessMessageMixin:
    # Added this attribute in task '1 of 4'
    success_message = ''

    # Added this method in task '2 of 4'
    def get_success_message(self):
        return self.success_message

1 Answer

Chris Komaroff
PLUS
Chris Komaroff
Courses Plus Student 14,198 Points

Weird, it works now, I just totally retyped everything. I think copy and paste caused a problem in the challenge. Sorry for false alarm.