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

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

What little thing am I missing? "Bummer! Hmm, didn't get the right success message from `ArticleCreate'

Challenge Link (cuz "Get Help" button still doesn't work) -> https://teamtreehouse.com/library/django-classbased-views/customizing-classbased-views/custom-mixin

Task 4 of 4

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!"


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):
        message.success(self.request, get_success_message)
        return super().form_valid(form)

I feel everything is correct... but still there must be one little mistake I'm blind to. :)

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

So, Kenneth Love ... a mistake in the challenge or a mistake in the code? :smiley:

4 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Your mixin never calls its own get_success_message method, it just references it. You need to actually call it (with parentheses). Next to last line in your pasted code.

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

But the error I'm getting is for the first 'hard-coded' string not being correct. Is there a method call there?

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Got it!!! Thanks Goodness. Thank-you Kenneth Love!

It was that, but I was also missing an 's' on "messages" in messages.success...

I can finally go to sleep! :dizzy:

Nicolas Baranowski
Nicolas Baranowski
1,992 Points

Hi Kenneth Love ,

I'm actually in the same situation as Jason was: I'm not calling get_success_message method. Could you please be a bit more specific on how I should do it?

Thanks :)

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Nicolas Baranowski the form_valid method needs to call the get_success_message method to get the success message.

Sorry what confusing in this challenge that it passes in Challenge 3 without this(so you assume that everything is fine here): def form_valid(self, form): message.success(self.request, self.get_success_message()) return super().form_valid(form)

And in Challenge 4 it looks like you've been asked just to edit articles/views.py

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That snippet you included looks fine except it's messages. I'll double check the validator for that.

I added a few more checks to the validator for that step. Hope that helps!

Thanks, Kenneth!!!

Love your challenges ;)

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Mistake in the code, you never called your method.

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Kenneth Love Could you elaborate juuuust a bit more... :)

I think I've been staring at this for too long today.