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

Unable to override mixin attribute 'success_message' correctly in challenge task

Challenge: '... In ArticleCreate, set the success_message message to "Article created!" ...'

Error: 'Bummer! Hmm, didn't get the right success message from ArticleCreate'

Seems similar to error 2 steps ago where I just retyped 2 lines of code (exactly as before) and it passed. Illegar characters? I even tried the setattr() function just for fun. Any ideas? Thanks!

class ArticleCreate(LoginRequiredMixin, mixins.SuccessMessageMixin, generic.CreateView):
    fields = ('title', 'body', 'author', 'published')
    model = models.Article
    success_message = "Article created!"
    #setattr(mixins.SuccessMessageMixin, success_message, "Article created!")
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, mixins.SuccessMessageMixin, generic.CreateView):
    fields = ('title', 'body', 'author', 'published')
    model = models.Article
    success_message = "Article created!"
    #setattr(mixins.SuccessMessageMixin, success_message, "Article created!")


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

    def get_success_message(self):
        obj = self.get_object()
        return "{} updated!".format(obj.title)


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:
    success_message = ''

    def get_success_message(self):
        return self.success_message

    def form_valid(self, form):
        messages.success(self.request, self.get_success_message())
        return super(ModelFormMixin, self).form_valid(form)

1 Answer

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

The original form_valid() method I wrote for SucessMessageMixin passed task 3 of 4, but caused 4 of 4 to fail. I fixed as as shows below.

articls/mixins.py

class SuccessMessageMixin:
    success_message = ''

    def get_success_message(self):
        return self.success_message

    # This mixin method caused problem in 4 of 4
    def form_valid(self, form):
        messages.success(self.request, self.get_success_message())
        #return super(ModelFormMixin, self).form_valid(form)  # No (passed 3 of 4, fails 4 of 4)
        return super().form_valid(form)  # Yes  (passes 3 of 4, passes 4 of 4)

I guess 'return super().form_valid(form)' is correct logic. It follows the djangoal PageTitleMixin example video.