**Reproduce**
Add following mixin to the auth/mixins.py
{{{
class LoginRequiredExemptMixin(object):
login_required_exempt = True
}}}
And import it at auth/views.py and use it on one of the CBV. For instance
LoginView.
{{{
from django.contrib.auth.mixins import LoginRequiredExemptMixin
class LoginView(SuccessURLAllowedHostsMixin, FormView,
LoginRequiredExemptMixin):
"""
Display the login form and handle the login action.
"""
}}}
In that case, you will be seeing following ImportError
{{{
File "/Users/xxx/PycharmProjects/django/django/contrib/auth/mixins.py",
line 4, in <module>
from django.contrib.auth.views import redirect_to_login
File "/Users/xxx/PycharmProjects/django/django/contrib/auth/views.py",
line 13, in <module>
from django.contrib.auth.mixins import LoginRequiredExemptMixin
ImportError: cannot import name 'LoginRequiredExemptMixin' from
'django.contrib.auth.mixins'
(/Users/mince/PycharmProjects/django/django/contrib/auth/mixins.py)
}}}
**How To Fix**
The reason of the error is mixins.py file have "django.contrib.auth.views
import redirect_to_login" on line 4. Since the redirect_to_login is only
used one place (handle_no_permission function) in mixin.py we can remove
the import from top of the file and call it right before usage of the
imported function. For example as follow.
{{{
def handle_no_permission(self):
if self.raise_exception or self.request.user.is_authenticated:
raise PermissionDenied(self.get_permission_denied_message())
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(self.request.get_full_path(),
self.get_login_url(), self.get_redirect_field_name())
}}}
On the other hand, I think why we have **SuccessURLAllowedHostsMixin**
class in auth/views.py file even though it's been called Mixin was related
that import code in mixin.py too.
By doing this changes we can even move **SuccessURLAllowedHostsMixin**
class to the mixins.py file since it's only being used in auth/views.py.
That little changes gives us flexibility to import mixings in the feature.
--
Ticket URL: <https://code.djangoproject.com/ticket/31367>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* has_patch: 0 => 1
* version: 3.0 => master
Comment:
https://github.com/django/django/pull/12577/
--
Ticket URL: <https://code.djangoproject.com/ticket/31367#comment:1>
* status: new => closed
* resolution: => wontfix
Comment:
Thanks for this ticket, however `django.contrib.auth.mixins` contains
[https://docs.djangoproject.com/en/3.0/topics/auth/default/ documented]
mixins that can be used with CBVs. `SuccessURLAllowedHostsMixin` is not
one of them, it's an internal mixin specific for login/logout views, so
currently there is no need to import anything from
`django.contrib.auth.mixins` in the `django.contrib.auth.views`.
--
Ticket URL: <https://code.djangoproject.com/ticket/31367#comment:2>