auth模块实现功能

通过入口url.py文件中定义的urlpatterns可以看出,auth模块共定义了8个url,分别用于:

  • 登录
  • 注销

  • 修改密码

  • 修改密码完成

  • 密码重置

  • 密码重置完成
  • 密码重置验证
  • 密码重置结束 ```
  1. from django.conf.urls import url
  2. from django.contrib.auth import views
  3. urlpatterns直接是一个list即可

  4. urlpatterns = [
  5. url(r’^login/$’, views.login, name=’login’),
  6. url(r’^logout/$’, views.logout, name=’logout’),
  7. url(r’^password_change/$’, views.password_change, name=’password_change’),
  8. url(r’^password_change/done/$’, views.password_change_done, name=’password_change_done’),
  9. url(r’^password_reset/$’, views.password_reset, name=’password_reset’),
  10. url(r’^password_reset/done/$’, views.password_reset_done, name=’password_reset_done’),
  11. url(r’^reset/(?P[0-9A-Za-z_-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$’,
  12. views.password_reset_confirm, name=’password_reset_confirm’),
  13. url(r’^reset/done/$’, views.password_reset_complete, name=’password_reset_complete’),
  14. ] 以下仅对登陆, 注销进行了学习.python

class SuccessURLAllowedHostsMixin: success_url_allowed_hosts = set()

  1. def get_success_url_allowed_hosts(self):
  2. return {self.request.get_host(), *self.success_url_allowed_hosts}

class LoginView(SuccessURLAllowedHostsMixin, FormView): “”” Display the login form and handle the login action. “”” form_class = AuthenticationForm authentication_form = None redirect_field_name = REDIRECT_FIELD_NAME template_name = ‘registration/login.html’ redirect_authenticated_user = False extra_context = None

  1. @method_decorator(sensitive_post_parameters())
  2. @method_decorator(csrf_protect)
  3. @method_decorator(never_cache)
  4. def dispatch(self, request, *args, **kwargs):
  5. if self.redirect_authenticated_user and self.request.user.is_authenticated:
  6. redirect_to = self.get_success_url()
  7. if redirect_to == self.request.path:
  8. raise ValueError(
  9. "Redirection loop for authenticated user detected. Check that "
  10. "your LOGIN_REDIRECT_URL doesn't point to a login page."
  11. )
  12. return HttpResponseRedirect(redirect_to)
  13. return super().dispatch(request, *args, **kwargs)
  14. def get_success_url(self):
  15. url = self.get_redirect_url()
  16. return url or resolve_url(settings.LOGIN_REDIRECT_URL)
  17. def get_redirect_url(self):
  18. """Return the user-originating redirect URL if it's safe."""
  19. redirect_to = self.request.POST.get(
  20. self.redirect_field_name,
  21. self.request.GET.get(self.redirect_field_name, '')
  22. )
  23. url_is_safe = url_has_allowed_host_and_scheme(
  24. url=redirect_to,
  25. allowed_hosts=self.get_success_url_allowed_hosts(),
  26. require_https=self.request.is_secure(),
  27. )
  28. return redirect_to if url_is_safe else ''
  29. def get_form_class(self):
  30. return self.authentication_form or self.form_class
  31. def get_form_kwargs(self):
  32. kwargs = super().get_form_kwargs()
  33. kwargs['request'] = self.request
  34. return kwargs
  35. def form_valid(self, form):
  36. """Security check complete. Log the user in."""
  37. auth_login(self.request, form.get_user())
  38. return HttpResponseRedirect(self.get_success_url())
  39. def get_context_data(self, **kwargs):
  40. context = super().get_context_data(**kwargs)
  41. current_site = get_current_site(self.request)
  42. context.update({
  43. self.redirect_field_name: self.get_redirect_url(),
  44. 'site': current_site,
  45. 'site_name': current_site.name,
  46. **(self.extra_context or {})
  47. })
  48. return context

class LogoutView(SuccessURLAllowedHostsMixin, TemplateView): “”” Log out the user and display the ‘You are logged out’ message. “”” next_page = None redirect_field_name = REDIRECT_FIELD_NAME template_name = ‘registration/logged_out.html’ extra_context = None

  1. @method_decorator(never_cache)
  2. def dispatch(self, request, *args, **kwargs):
  3. auth_logout(request)
  4. next_page = self.get_next_page()
  5. if next_page:
  6. # Redirect to this page until the session has been cleared.
  7. return HttpResponseRedirect(next_page)
  8. return super().dispatch(request, *args, **kwargs)
  9. def post(self, request, *args, **kwargs):
  10. """Logout may be done via POST."""
  11. return self.get(request, *args, **kwargs)
  12. def get_next_page(self):
  13. if self.next_page is not None:
  14. next_page = resolve_url(self.next_page)
  15. elif settings.LOGOUT_REDIRECT_URL:
  16. next_page = resolve_url(settings.LOGOUT_REDIRECT_URL)
  17. else:
  18. next_page = self.next_page
  19. if (self.redirect_field_name in self.request.POST or
  20. self.redirect_field_name in self.request.GET):
  21. next_page = self.request.POST.get(
  22. self.redirect_field_name,
  23. self.request.GET.get(self.redirect_field_name)
  24. )
  25. url_is_safe = url_has_allowed_host_and_scheme(
  26. url=next_page,
  27. allowed_hosts=self.get_success_url_allowed_hosts(),
  28. require_https=self.request.is_secure(),
  29. )
  30. # Security check -- Ensure the user-originating redirection URL is
  31. # safe.
  32. if not url_is_safe:
  33. next_page = self.request.path
  34. return next_page
  35. def get_context_data(self, **kwargs):
  36. context = super().get_context_data(**kwargs)
  37. current_site = get_current_site(self.request)
  38. context.update({
  39. 'site': current_site,
  40. 'site_name': current_site.name,
  41. 'title': _('Logged out'),
  42. **(self.extra_context or {})
  43. })
  44. return context

```