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

Derrick Kearney
Derrick Kearney
2,962 Points

Error with CBVs mixins, part 4, wrong "success message from `ArticleCreate`"

I am not seeing the error and the error msg is not helping me locate it. I am sure it is something small and I am just missing it.

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


class ArticleUpdate(LoginRequiredMixin, generic.UpdateView, mixins.SuccessMessageMixin):
    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().form_valid(form)

1 Answer

The only difference that I can see between my code and yours is that my super is a bit different. You could try modifying that.

articles/mixins.py

from django.contrib import messages


class SuccessMessageMixin:
    success_message = ''

    def form_valid(self, form):
        messages.success(self.request, self.get_success_message())
        return super(SuccessMessageMixin, self).form_valid(form)  # Difference here

    def get_success_message(self):
        return self.success_message

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


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

    def get_success_message(self):
        return '{} updated!'.format(self.get_object().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()
Derrick Kearney
Derrick Kearney
2,962 Points

Thanks, that worked perfect.