基于How To Tango With Django 1.9的重新实践(11)——User Authentication with {#chapter-redux}

来源:互联网 发布:淘宝规则 新规 编辑:程序博客网 时间:2024/06/06 19:43

在上一章中,我们通过手动编写URL,视图和模板来添加登录和注册功能。然而,这样的功能是许多web应用程序所共有的,所以开发人员已经创建了许多附加应用程序,可以包括在您的Django项目中,以减少提供登录,注册,一步和两步验证所需的代码量,密码chaning,密码恢复等。在本章中,我们将使用包django-registration-redux提供这些设施。

这意味着我们需要重新考虑我们的代码,以删除我们以前创建的登录和注册功能,然后设置和配置我们的项目以包括应用django-registration-redux程序。本章还将为您提供使用外部应用程序的一些经验,并告诉您如何轻松地将它们插入到您的Django项目中。

11.1 设置DRR

开始我们先安装django-registration-redux:

$ pip install django-registration-redux

安装好后,我们需要告诉Django我们使用这个应用.打开settings.py文件,修改INSTALLED_APPS元组:

INSTALLED_APPS = ('django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','rango','registration', # add in the registration package)

同时在settings.py文件你可以加入:

REGISTRATION_OPEN = True                # If True, users can registerACCOUNT_ACTIVATION_DAYS = 7     # One-week activation window; you may, of course, use a different value.REGISTRATION_AUTO_LOGIN = True  # If True, the user will be automatically logged in.LOGIN_REDIRECT_URL = '/rango/'  # The page you want users to arrive at after they successful log inLOGIN_URL = '/accounts/login/'  # The page users are directed to if they are not logged in,                                                                # and are trying to access pages requiring authentication

上面这些设置都带了注释.现在在tango_with_django_project/urls.py里修改urlpatterns以增加注册包的引用:

from django.conf.urls import url,includefrom django.contrib import adminfrom django.conf import settingsfrom django.conf.urls.static import staticurlpatterns = [    url(r'^admin/', include(admin.site.urls)),    url(r'^rango/', include('rango.urls')),    url(r'^accounts/', include('registration.backends.simple.urls')),]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

django-registration-redux包提供了许多不同的注册后台供你选择.在registration.backend.simple.urls,它提供了:

  • registration -> /accounts/register/
  • registration complete -> /accounts/register/complete
  • login -> /accounts/login/
  • logout -> /accounts/logout/
  • password change -> /password/change/
  • password reset -> /password/reset/

在registration.backends.default.urls里也提供了两步激活账户的功能:

  • 全面激活(使用两步注册) -> ctivate/complete/
  • 激活(如果账户激活失败) ->ctivate/<activation_key>/
  • 激活邮件(通知用户激活邮件已经发送)
    • 激活邮件主体(文本文件,包含激活邮件的文本)
    • 激活邮件标题(文本文件,包含激活文件的标题)

注意Django Registration Redux只提供功能,没有提供模板.所以我们需要自己写模板来连接每个视图.

11.3 设置模板

在这篇 https://django-registration-redux.readthedocs.org/en/latest/quickstart.html 快速入门里只提供了一般的模板需求,没有清楚的说明每个模板都应当包含什么.

在这里我们可以下载Anders Hofstee的Github项目,https://github.com/macdhuibh/django-registration-templates ,你可以在这里找到详细的模板.我们将使用这个模板作为我们的指导.

首先,我们需要在templates目录新建一个registration目录.这里将存储所有关于 Django Registration Redux应用的页面.

11.3.1 登录模板

在templates/registration创建login.html文件,代码如下:

{% extends "rango/base.html" %}{% block body_block %}<h1>Login</h1>        <form method="post" action=".">                {% csrf_token %}                {{ form.as_p }}                <input type="submit" value="Log in" />                <input type="hidden" name="next" value="{{ next }}" />                </form>        <p>Not  a member? <a href="{% url 'registration_register' %}">Register</a>!</p>{% endblock %}

注意每个url引用都要用url模板标签来引用.如果你访问 http://127.0.0.1:8000/accounts/ 你就会看到绑定的url列表,每个列表后边都有它的名字.

11.3.2 注册模板

在templates/registration创建registration_form.html文件:

{% extends "rango/base.html" %}{% block body_block %}<h1>Register Here</h1>        <form method="post" action=".">                {% csrf_token %}                {{ form.as_p }}                <input type="submit" value="Submit" />        </form>{% endblock %}

11.3.3 注册完成模板

在templates/registration创建registration_complete.html文件:

{% extends "rango/base.html" %}{% block body_block %}<h1>Registration Complete</h1>        <p>You are now registered</p>{% endblock %}

12.3.4 注销模板
在templates/registration目录创建logout.html文件:

{% extends "rango/base.html" %}{% block body_block %}<h1>Logged Out</h1>        <p>You are now logged out.</p>{% endblock %}

11.3.5 尝试注册

运行Django服务器,访问: http://127.0.0.1:8000/accounts/register/

注意到注册表单包含两个密码字段 - 它可以进行检车.尝试输入不同的密码.

它能够正常的运行,但并不是所有的东西都做好了,我们还需要一些逻辑代码.

这里写图片描述

11.3.6 重构项目

现在需要修改bashe.html来使用心得注册url/视图:

  • 修改注册url为<a href="{% url 'registration_register' %}">
  • 登录为<a href="{% url 'auth_login' %}">
  • 注销为<a href="{% url 'auth_logout' %}?next=/rango/">

修改settings.py中的LOGIN_URL为/accounts/login/.
注意到注销页面我们包含了?next=/rango/.这就是为什么当我们注销时会重定向到rango主页.如果我们不加入它,我们将会重定向到注销页面(但是这样做不太友好).

下面就是解绑rango应用的register,login,logout的功能,例如删除urls,视图和模板(或者把它们注释掉)

11.3.7 修改注册流程

这时候如果用户进行注册,完成后它会重定向到注册完成页面.这有些笨重,我们可以直接重定向到主页.我们可以通过重写registration.backends.simple.views的RegistrationView来实现这个功能.好了,现在在tango_with_django_project/urls.py文件里引入RegistrationView,增加一个新的注册类,然后更新urlpatterns如下:

from registration.backends.simple.views import RegistrationView# Create a new class that redirects the user to the index page, if successful at loggingclass MyRegistrationView(RegistrationView):    def get_success_url(self,request, user):        return '/rango/'urlpatterns = [    url(r'^admin/', include(admin.site.urls)),    url(r'^rango/', include('rango.urls')),    #这将允许accounts/register在任何其他accounts/URL之前匹配。    #这允许我们重定向account/register到我们的自定义注册视图。    url(r'^accounts/register/$',MyRegistrationView.as_view(),name='registration_register'),]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Exercise

  • 为用户提供修改密码的功能.

Hints

  • 更改密码的URL是accounts / password / change /,表示密码已更改的URL是:accounts / password / change / done /
0 0
原创粉丝点击