TIL/2020–11–30

Renzo Regio
2 min readNov 30, 2020

Today I learned about Django’s SuccessMessageMixin. It is actually quite simple and it does the same function of messages. But I think it is quite a useful tool especially if you don’t want to have too much lines of code by doing messages.

Mixins basically are a lot. It is not only SuccessMessageMixin but there are quite a few like LoginRequiredMixin and UserPassesTestMixin. Mixins allows you to add another argument to your class that gives you more functionality.

On this TIL I will talk about the SuccessMessageMixin. As I said, SuccessMessageMixin provides the same function as messages. It provides a message but the limitation is that it will only provide a success message. We cannot make it into error, info, debug, etc. Similarly to using other Django functions, there are some limitations.

  1. Import: from django.contrib.messages.views import SuccessMessageMixin
  2. Add SuccessMessageMixin on the argument of your CBV
  3. add “success_message = “” “ in your CBV

For example, on my EditProfileView

And every time the form is valid — every time a user submits the form — the success_message will show up after the form redirects. Similarly to the method of success_url.

Mixins are quite useful. I know this mixin seems unnecessary, like it is only an additional feature that can or can not be part of the experience but there are other mixins that would definitely be needed. And would definitely protect the website as well. I will talk about those mixins on the next one!

--

--