[Django] #33357: LoginView combine SuccessMessageMixin failed!

15 views
Skip to first unread message

Django

unread,
Dec 11, 2021, 8:28:36 AM12/11/21
to django-...@googlegroups.com
#33357: LoginView combine SuccessMessageMixin failed!
-------------------------------------+-------------------------------------
Reporter: fincan | Owner: nobody
Type: Bug | Status: new
Component: | Version: 4.0
contrib.messages | Keywords: LoginView,
Severity: Normal | messages, SuccessMessageMixin
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

---------------------
project urls:

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
path('', include('base.urls')),
]
---------------------

the base apps urls:
# app_name = 'base'

urlpatterns = [
path('', views.Home.as_view(), name='home'),
path('room/<int:pk>/', views.Room.as_view(), name='room'),
path('create-room/', views.CreateRoom.as_view(), name='create-room'),
path('update-room/<int:pk>/', views.UpdateRoom.as_view(model=Room),
name='update-room'),
path('delete-room/<int:pk>/', views.DeleteRoom.as_view(model=Room),
name='delete-room'),
path('login/', views.Login.as_view(), name='login')
]
---------------------

settings.py


INSTALLED_APPS = [
# Default django apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

# My custom apps
'base.apps.BaseConfig',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'studybud.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates']
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'studybud.wsgi.application'

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}

# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-
validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME':
'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME':
'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME':
'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME':
'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# Login redirect
LOGIN_REDIRECT_URL = 'home'
LOGIN_URL = 'login'

--
Ticket URL: <https://code.djangoproject.com/ticket/33357>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 11, 2021, 8:49:19 AM12/11/21
to django-...@googlegroups.com
#33357: SuccessMessageMixin doesn't work with LoginView
-------------------------------------+-------------------------------------
Reporter: fincan | Owner: Baptiste
| Mispelon
Type: Bug | Status: assigned
Component: contrib.auth | Version: 4.0
Severity: Normal | Resolution:
Keywords: LoginView, | Triage Stage: Accepted
messages, SuccessMessageMixin |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Baptiste Mispelon):

* status: new => assigned
* cc: Baptiste Mispelon (added)
* component: contrib.messages => contrib.auth
* owner: nobody => Baptiste Mispelon
* stage: Unreviewed => Accepted


Old description:

New description:

I have a problem with `SuccessMessageMixin, LoginView`. There is no
problem with `CreateView`:
{{{#!py


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`
{{{#!py


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

I can not capture 'Message' with `LoginView`.

--

Comment:

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()`:
{{{#!py
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:
{{{#!py
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/33357#comment:1>

Django

unread,
Dec 11, 2021, 9:48:28 AM12/11/21
to django-...@googlegroups.com
#33357: SuccessMessageMixin doesn't work with LoginView
-------------------------------------+-------------------------------------
Reporter: Cihat Ertem | Owner: Baptiste
| Mispelon
Type: Bug | Status: closed
Component: contrib.messages | Version: 4.0
Severity: Normal | Resolution: needsinfo

Keywords: LoginView, | Triage Stage: Accepted
messages, SuccessMessageMixin |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Baptiste Mispelon):

* status: assigned => closed
* resolution: => needsinfo
* component: contrib.auth => contrib.messages


Old description:

> I have a problem with `SuccessMessageMixin, LoginView`. There is no
> problem with `CreateView`:
> {{{#!py


> 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`
> {{{#!py


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

> I can not capture 'Message' with `LoginView`.

New description:

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

{{{#!py
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

{{{#!py
class Login(SuccessMessageMixin, LoginView):

template_name = 'registration/login.html'
success_url = reverse_lazy('home')
success_message = 'Message!!!'
}}}

I can not capture 'Message' with LoginView.

project urls:
{{{#!py

urlpatterns = [

path('admin/', admin.site.urls),
path(, include('django.contrib.auth.urls')),
path(, include('base.urls')),

]
}}}

the base apps urls:
{{{#!py
# app_name = 'base'

urlpatterns = [

path(, views.Home.as_view(), name='home'),


path('room/<int:pk>/', views.Room.as_view(), name='room'),
path('create-room/', views.CreateRoom.as_view(), name='create-room'),
path('update-room/<int:pk>/', views.UpdateRoom.as_view(model=Room),
name='update-room'),
path('delete-room/<int:pk>/', views.DeleteRoom.as_view(model=Room),
name='delete-room'),
path('login/', views.Login.as_view(), name='login')

]
}}}

settings.py:
{{{#!py

INSTALLED_APPS = [

]

MIDDLEWARE = [

]

ROOT_URLCONF = 'studybud.urls'

TEMPLATES = [

{

'context_processors': [

],

},

},

]

WSGI_APPLICATION = 'studybud.wsgi.application'

DATABASES = {

'default': {

}

}

AUTH_PASSWORD_VALIDATORS = [

{

'NAME':
'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',

},
{

'NAME':
'django.contrib.auth.password_validation.MinimumLengthValidator',

},
{

'NAME':
'django.contrib.auth.password_validation.CommonPasswordValidator',

},
{

'NAME':
'django.contrib.auth.password_validation.NumericPasswordValidator',

},

]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

STATIC_URL = 'static/'

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

--

Comment:

It turns out my initial tests were incorrect and I cannot actually
reproduce your issue.

Using `class MyView(SuccessMessageMixin, LoginView)` works fine for me,
there must be something else that's broken in your setup.

I've marked the ticket as "needs info" and restored your original
description. Could you reopen the ticket with some details on how to
reproduce your issue?
In particular, how do you display the messages in your templates.

Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/33357#comment:2>

Django

unread,
Dec 11, 2021, 12:58:50 PM12/11/21
to django-...@googlegroups.com
#33357: SuccessMessageMixin doesn't work with LoginView
-------------------------------------+-------------------------------------
Reporter: Cihat Ertem | Owner: Baptiste
| Mispelon
Type: Bug | Status: closed
Component: contrib.messages | Version: 4.0
Severity: Normal | Resolution: fixed

Keywords: LoginView, | Triage Stage: Accepted
messages, SuccessMessageMixin |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Cihat Ertem):

* resolution: needsinfo => fixed


Comment:

Ok, I found my fault!
I changed urlpattern order from this:
...


urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
path('', include('base.urls')),
]

to this:


urlpatterns = [
path('admin/', admin.site.urls),

path('', include('base.urls')),


path('', include('django.contrib.auth.urls')),

]

Thanks for your replies. Maybe a note should be add for order priority to
doc. section.

--
Ticket URL: <https://code.djangoproject.com/ticket/33357#comment:3>

Django

unread,
Dec 11, 2021, 2:14:13 PM12/11/21
to django-...@googlegroups.com
#33357: SuccessMessageMixin doesn't work with LoginView
-------------------------------------+-------------------------------------
Reporter: Cihat Ertem | Owner: Baptiste
| Mispelon
Type: Bug | Status: closed
Component: contrib.messages | Version: 4.0
Severity: Normal | Resolution: invalid

Keywords: LoginView, | Triage Stage: Accepted
messages, SuccessMessageMixin |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* resolution: fixed => invalid


--
Ticket URL: <https://code.djangoproject.com/ticket/33357#comment:4>

Reply all
Reply to author
Forward
0 new messages