Opened 3 years ago

Last modified 3 years ago

#33357 closed Bug

SuccessMessageMixin doesn't work with LoginView — at Version 1

Reported by: Cihat Ertem Owned by: Baptiste Mispelon
Component: contrib.messages Version: 4.0
Severity: Normal Keywords: LoginView, messages, SuccessMessageMixin
Cc: Baptiste Mispelon Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Baptiste Mispelon)

I have a problem with SuccessMessageMixin, LoginView. There is no problem with CreateView:

class CreateRoom(SuccessMessageMixin, CreateView):
    template_name = 'base/room_form.html'
    form_class = RoomForm
    success_url = reverse_lazy('home')
    success_message = 'Message'

I can capture and use 'Message' in template but with LoginView

class Login(SuccessMessageMixin, LoginView):
    template_name = 'registration/login.html'
    success_url = reverse_lazy('home')
    success_message = 'Message!!!'

I can not capture 'Message' with LoginView.

Change History (1)

comment:1 by Baptiste Mispelon, 3 years ago

Cc: Baptiste Mispelon added
Component: contrib.messagescontrib.auth
Description: modified (diff)
Owner: changed from nobody to Baptiste Mispelon
Status: newassigned
Summary: LoginView combine SuccessMessageMixin failed!SuccessMessageMixin doesn't work with LoginView
Triage Stage: UnreviewedAccepted

Hi,

Thanks for reporting this issue. I've taken the liberty to edit your original report to remove information that I don't think is relevant, I hope that's ok.

The issue comes from the fact that LoginView.form_valid() doesn't call super():

    def form_valid(self, form):
        """Security check complete. Log the user in."""
        auth_login(self.request, form.get_user())
        return HttpResponseRedirect(self.get_success_url())

I can't find a documented reason why and changing it to this doesn't break any tests:

    def form_valid(self, form):
        """Security check complete. Log the user in."""
        auth_login(self.request, form.get_user())
        return super().form_valid(form)

That change should fix the reported issue and seems straightforward enough.

Note: See TracTickets for help on using tickets.
Back to Top