django mixin 以确保用户处于活动状态
django mixin to make sure that user is active
我需要确保经过身份验证的用户可以访问该页面。 django 中是否有任何 mixin?如果不是那么该怎么做。提前致谢。
它对我来说很好用
from django.contrib.auth.mixins import AccessMixin
from django.contrib import messages
from django.core.exceptions import PermissionDenied, ImproperlyConfigured
from django.shortcuts import redirect
from django.contrib.auth.views import redirect_to_login
class ActiveOnlyMixin(AccessMixin):
permission_denied_message = ''
not_activated_message = ''
not_activated_redirect = ''
def get_not_activated_message(self):
return self.not_activated_message
def handle_not_activated(self):
message = self.get_not_activated_message()
if self.raise_exception:
raise PermissionDenied(message)
messages.error(self.request, message)
return redirect(self.get_not_activated_redirect())
def get_not_activated_redirect(self):
if not self.not_activated_redirect:
raise ImproperlyConfigured(
'{0} is missing the not_activated_redirect attribute. Define {0}.not_activated_redirect, or override '
'{0}.get_not_activated_redirect().'.format(self.__class__.__name__))
return self.not_activated_redirect
def handle_no_permission(self):
message = self.get_permission_denied_message()
if self.raise_exception:
raise PermissionDenied(message)
messages.error(self.request, message)
return redirect_to_login(self.request.get_full_path(), self.get_login_url(), self.get_redirect_field_name())
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return self.handle_no_permission()
if not request.user.is_active:
return self.handle_not_activated()
return super(ActiveOnlyMixin, self).dispatch(request, *args, **kwargs)
并在 views.py
from myapp.mixins import ActiveOnlyMixin
class MyView(ActiveOnlyMixin, View):
permission_denied_message = 'You must be logged in to view this page'
not_activated_message = 'You haven\'t activated your account yet'
not_activated_redirect = 'accounts:inactive_registration'
我需要确保经过身份验证的用户可以访问该页面。 django 中是否有任何 mixin?如果不是那么该怎么做。提前致谢。
它对我来说很好用
from django.contrib.auth.mixins import AccessMixin
from django.contrib import messages
from django.core.exceptions import PermissionDenied, ImproperlyConfigured
from django.shortcuts import redirect
from django.contrib.auth.views import redirect_to_login
class ActiveOnlyMixin(AccessMixin):
permission_denied_message = ''
not_activated_message = ''
not_activated_redirect = ''
def get_not_activated_message(self):
return self.not_activated_message
def handle_not_activated(self):
message = self.get_not_activated_message()
if self.raise_exception:
raise PermissionDenied(message)
messages.error(self.request, message)
return redirect(self.get_not_activated_redirect())
def get_not_activated_redirect(self):
if not self.not_activated_redirect:
raise ImproperlyConfigured(
'{0} is missing the not_activated_redirect attribute. Define {0}.not_activated_redirect, or override '
'{0}.get_not_activated_redirect().'.format(self.__class__.__name__))
return self.not_activated_redirect
def handle_no_permission(self):
message = self.get_permission_denied_message()
if self.raise_exception:
raise PermissionDenied(message)
messages.error(self.request, message)
return redirect_to_login(self.request.get_full_path(), self.get_login_url(), self.get_redirect_field_name())
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return self.handle_no_permission()
if not request.user.is_active:
return self.handle_not_activated()
return super(ActiveOnlyMixin, self).dispatch(request, *args, **kwargs)
并在 views.py
from myapp.mixins import ActiveOnlyMixin
class MyView(ActiveOnlyMixin, View):
permission_denied_message = 'You must be logged in to view this page'
not_activated_message = 'You haven\'t activated your account yet'
not_activated_redirect = 'accounts:inactive_registration'