Changes between Initial Version and Version 1 of Ticket #33357


Ignore:
Timestamp:
Dec 11, 2021, 7:49:17 AM (2 years ago)
Author:
Baptiste Mispelon
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():

    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.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #33357

    • Property Cc Baptiste Mispelon added
    • Property Component contrib.messagescontrib.auth
    • Property Owner changed from nobody to Baptiste Mispelon
    • Property Triage Stage UnreviewedAccepted
    • Property Status newassigned
    • Property Summary LoginView combine SuccessMessageMixin failed!SuccessMessageMixin doesn't work with LoginView
  • Ticket #33357 – Description

    initial v1  
    1 I have a problem with SuccessMessageMixin, LoginView. There is no problem with CreateView:
     1I have a problem with `SuccessMessageMixin, LoginView`. There is no problem with `CreateView`:
     2{{{#!py
    23class CreateRoom(SuccessMessageMixin, CreateView):
    34    template_name = 'base/room_form.html'
     
    56    success_url = reverse_lazy('home')
    67    success_message = 'Message'
     8}}}
    79
    8 I can capture and use 'Message' in template but with LoginView
     10I can capture and use 'Message' in template but with `LoginView`
     11{{{#!py
    912class Login(SuccessMessageMixin, LoginView):
    1013    template_name = 'registration/login.html'
    1114    success_url = reverse_lazy('home')
    1215    success_message = 'Message!!!'
     16}}}
    1317
    14 I can not capture 'Message' with LoginView.
    15 
    16 ---------------------
    17 project urls:
    18 
    19 urlpatterns = [
    20     path('admin/', admin.site.urls),
    21     path('', include('django.contrib.auth.urls')),
    22     path('', include('base.urls')),
    23 ]
    24 ---------------------
    25 
    26 the base apps urls:
    27 # app_name = 'base'
    28 
    29 urlpatterns = [
    30     path('', views.Home.as_view(), name='home'),
    31     path('room/<int:pk>/', views.Room.as_view(), name='room'),
    32     path('create-room/', views.CreateRoom.as_view(), name='create-room'),
    33     path('update-room/<int:pk>/', views.UpdateRoom.as_view(model=Room), name='update-room'),
    34     path('delete-room/<int:pk>/', views.DeleteRoom.as_view(model=Room), name='delete-room'),
    35     path('login/', views.Login.as_view(), name='login')
    36 ]
    37 ---------------------
    38 
    39 settings.py
    40 
    41 
    42  INSTALLED_APPS = [
    43     # Default django apps
    44     'django.contrib.admin',
    45     'django.contrib.auth',
    46     'django.contrib.contenttypes',
    47     'django.contrib.sessions',
    48     'django.contrib.messages',
    49     'django.contrib.staticfiles',
    50 
    51     # My custom apps
    52     'base.apps.BaseConfig',
    53 ]
    54 
    55 MIDDLEWARE = [
    56     'django.middleware.security.SecurityMiddleware',
    57     'django.contrib.sessions.middleware.SessionMiddleware',
    58     'django.middleware.common.CommonMiddleware',
    59     'django.middleware.csrf.CsrfViewMiddleware',
    60     'django.contrib.auth.middleware.AuthenticationMiddleware',
    61     'django.contrib.messages.middleware.MessageMiddleware',
    62     'django.middleware.clickjacking.XFrameOptionsMiddleware',
    63 ]
    64 
    65 ROOT_URLCONF = 'studybud.urls'
    66 
    67 TEMPLATES = [
    68     {
    69         'BACKEND': 'django.template.backends.django.DjangoTemplates',
    70         'DIRS': [BASE_DIR / 'templates']
    71         ,
    72         'APP_DIRS': True,
    73         'OPTIONS': {
    74             'context_processors': [
    75                 'django.template.context_processors.debug',
    76                 'django.template.context_processors.request',
    77                 'django.contrib.auth.context_processors.auth',
    78                 'django.contrib.messages.context_processors.messages',
    79             ],
    80         },
    81     },
    82 ]
    83 
    84 WSGI_APPLICATION = 'studybud.wsgi.application'
    85 
    86 # Database
    87 # https://docs.djangoproject.com/en/4.0/ref/settings/#databases
    88 
    89 DATABASES = {
    90     'default': {
    91         'ENGINE': 'django.db.backends.sqlite3',
    92         'NAME': BASE_DIR / 'db.sqlite3',
    93     }
    94 }
    95 
    96 # Password validation
    97 # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
    98 
    99 AUTH_PASSWORD_VALIDATORS = [
    100     {
    101         'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    102     },
    103     {
    104         'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    105     },
    106     {
    107         'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    108     },
    109     {
    110         'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    111     },
    112 ]
    113 
    114 # Internationalization
    115 # https://docs.djangoproject.com/en/4.0/topics/i18n/
    116 
    117 LANGUAGE_CODE = 'en-us'
    118 
    119 TIME_ZONE = 'UTC'
    120 
    121 USE_I18N = True
    122 
    123 USE_TZ = True
    124 
    125 # Static files (CSS, JavaScript, Images)
    126 # https://docs.djangoproject.com/en/4.0/howto/static-files/
    127 
    128 STATIC_URL = 'static/'
    129 
    130 # Default primary key field type
    131 # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
    132 
    133 DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
    134 
    135 # Login redirect
    136 LOGIN_REDIRECT_URL = 'home'
    137 LOGIN_URL = 'login'
     18I can not capture 'Message' with `LoginView`.
Back to Top