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 Mixins

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

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

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

    # Code I added for 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!

If you link to the actual challenge instead of the video it might help.

3 Answers

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

Note on task 4 of 4

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.

Sorry folks if I prattle too much, but I hope these observations I made can be helpful to future students!

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

Hi jacinator, makes sense, I just opened a new identical question directly under the challenge question. I am going to delete this original question. Thank you for your time.

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

P.S. I think some people like to post questions about code challenges to the video because the code challenge does not show the question that you post against it.