楼主: jieforest

Django框架中的用户认证的实现

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
41#
 楼主| 发表于 2012-12-19 15:18 | 只看该作者
login_required() also takes an optional login_url parameter. Example:
  1. from django.contrib.auth.decorators import login_required

  2. @login_required(login_url='/accounts/login/')
  3. def my_view(request):
  4.     ...
复制代码
Note that if you don't specify the login_url parameter, you'll need to map the appropriate Django view to settings.LOGIN_URL. For example, using the defaults, add the following line to your URLconf:
  1. (r'^accounts/login/Changed in Django 1.5: Please see the release notes

  2. As of version 1.5 settings.LOGIN_URL now also accepts view function names and named URL patterns. This allows you to freely remap your login view within your URLconf without having to update the setting., 'django.contrib.auth.views.login'),
复制代码
Changed in Django 1.5: Please see the release notes

As of version 1.5 settings.LOGIN_URL now also accepts view function names and named URL patterns. This allows you to freely remap your login view within your URLconf without having to update the setting.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
42#
 楼主| 发表于 2012-12-19 15:21 | 只看该作者
views.login(request[, template_name, redirect_field_name, authentication_form])

URL name: login

See the URL documentation for details on using named URL patterns.

Here's what django.contrib.auth.views.login does:

If called via GET, it displays a login form that POSTs to the same URL. More on this in a bit.

If called via POST, it tries to log the user in. If login is successful, the view redirects to the URL specified in next. If nextisn't provided, it redirects to settings.LOGIN_REDIRECT_URL (which defaults to /accounts/profile/). If login isn't successful, it redisplays the login form.

It's your responsibility to provide the login form in a template called registration/login.html by default. This template gets passed four template context variables:

form: A Form object representing the login form. See the forms documentation for more on Form objects.

next: The URL to redirect to after successful login. This may contain a query string, too.

site: The current Site, according to the SITE_ID setting. If you don't have the site framework installed, this will be set to an instance of RequestSite, which derives the site name and domain from the current HttpRequest.

site_name: An alias for site.name. If you don't have the site framework installed, this will be set to the value ofrequest.META['SERVER_NAME']. For more on sites, see The "sites" framework.

If you'd prefer not to call the template registration/login.html, you can pass the template_name parameter via the extra arguments to the view in your URLconf. For example, this URLconf line would use myapp/login.html instead:
  1. (r'^accounts/login/, 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html'}),
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
43#
 楼主| 发表于 2012-12-20 21:08 | 只看该作者
本帖最后由 jieforest 于 2012-12-20 21:10 编辑

You can also specify the name of the GET field which contains the URL to redirect to after login by passing redirect_field_nameto the view. By default, the field is called next.

Here's a sample registration/login.html template you can use as a starting point. It assumes you have a base.html template that defines a content block:

  1. {% extends "base.html" %}

  2. {% block content %}

  3. {% if form.errors %}
  4. <p>Your username and password didn't match. Please try again.</p>
  5. {% endif %}

  6. <form method="post" action="{% url 'django.contrib.auth.views.login' %}">
  7. {% csrf_token %}
  8. <table>
  9. <tr>
  10.     <td>{{ form.username.label_tag }}</td>
  11.     <td>{{ form.username }}</td>
  12. </tr>
  13. <tr>
  14.     <td>{{ form.password.label_tag }}</td>
  15.     <td>{{ form.password }}</td>
  16. </tr>
  17. </table>

  18. <input type="submit" value="login" />
  19. <input type="hidden" name="next" value="{{ next }}" />
  20. </form>

  21. {% endblock %}
复制代码


使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
44#
 楼主| 发表于 2012-12-20 21:11 | 只看该作者
If you are using alternate authentication (see Other authentication sources) you can pass a custom authentication form to the login view via the authentication_form parameter. This form must accept a request keyword argument in its __init__method, and provide a get_user method which returns the authenticated user object (this method is only ever called after successful form validation).

New in Django 1.4: Please see the release notes

The login() view and the Other built-in views now all return a TemplateResponse instance, which allows you to easily customize the response data before rendering. For more details, see the TemplateResponse documentation.

Other built-in views

In addition to the login() view, the authentication system includes a few other useful built-in views located in django.contrib.auth.views:

logout(request[, next_page, template_name, redirect_field_name])

Logs a user out.

URL name: logout

See the URL documentation for details on using named URL patterns.

Optional arguments:

next_page: The URL to redirect to after logout.

template_name: The full name of a template to display after logging the user out. Defaults to registration/logged_out.html if no argument is supplied.

redirect_field_name: The name of a GET field containing the URL to redirect to after log out. Overrides next_page if the givenGET parameter is passed.

Template context:

title: The string "Logged out", localized.

site: The current Site, according to the SITE_ID setting. If you don't have the site framework installed, this will be set to an instance of RequestSite, which derives the site name and domain from the current HttpRequest.

site_name: An alias for site.name. If you don't have the site framework installed, this will be set to the value ofrequest.META['SERVER_NAME']. For more on sites, see The "sites" framework.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
45#
 楼主| 发表于 2012-12-20 21:12 | 只看该作者
logout_then_login(request[, login_url])

Logs a user out, then redirects to the login page.

URL name: No default URL provided

Optional arguments:

login_url: The URL of the login page to redirect to. Defaults to settings.LOGIN_URL if not supplied.

password_change(request[, template_name, post_change_redirect, password_change_form])
Allows a user to change their password.

URL name: password_change

Optional arguments:

template_name: The full name of a template to use for displaying the password change form. Defaults to registration/password_change_form.html if not supplied.

post_change_redirect: The URL to redirect to after a successful password change.

password_change_form: A custom "change password" form which must accept a user keyword argument. The form is responsible for actually changing the user's password. Defaults to PasswordChangeForm.

Template context:

form: The password change form (see password_change_form above).

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
46#
 楼主| 发表于 2012-12-20 21:13 | 只看该作者
password_change_done(request[, template_name])

The page shown after a user has changed their password.

URL name: password_change_done

Optional arguments:

template_name: The full name of a template to use. Defaults to registration/password_change_done.html if not supplied.

password_reset(request[, is_admin_site, template_name, email_template_name, password_reset_form,token_generator, post_reset_redirect, from_email])

Allows a user to reset their password by generating a one-time use link that can be used to reset the password, and sending that link to the user's registered email address.

Changed in Django 1.4: Users flagged with an unusable password (see set_unusable_password() will not be able to request a password reset to prevent misuse when using an external authentication source like LDAP.

URL name: password_reset

Optional arguments:

template_name: The full name of a template to use for displaying the password reset form. Defaults to registration/password_reset_form.html if not supplied.

email_template_name: The full name of a template to use for generating the email with the reset password link. Defaults to registration/password_reset_email.html if not supplied.

subject_template_name: The full name of a template to use for the subject of the email with the reset password link. Defaults to registration/password_reset_subject.txt if not supplied.

New in Django 1.4: Please see the release notes

password_reset_form: Form that will be used to get the email of the user to reset the password for. Defaults to PasswordResetForm.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
47#
 楼主| 发表于 2012-12-20 21:14 | 只看该作者
token_generator: Instance of the class to check the one time link. This will default to default_token_generator, it's an instance of django.contrib.auth.tokens.PasswordResetTokenGenerator.

post_reset_redirect: The URL to redirect to after a successful password reset request.

from_email: A valid email address. By default Django uses the DEFAULT_FROM_EMAIL.

Template context:

form: The form (see password_reset_form above) for resetting the user's password.

Email template context:

email: An alias for user.email

user: The current User, according to the email form field. Only active users are able to reset their passwords (User.is_active is True).
site_name: An alias for site.name. If you don't have the site framework installed, this will be set to the value of request.META['SERVER_NAME']. For more on sites, see The "sites" framework.

domain: An alias for site.domain. If you don't have the site framework installed, this will be set to the value ofrequest.get_host().

protocol: http or https

uid: The user's id encoded in base 36.

token: Token to check that the reset link is valid.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
48#
 楼主| 发表于 2012-12-21 22:29 | 只看该作者
Sample registration/password_reset_email.html (email body template):
  1. Someone asked for password reset for email {{ email }}. Follow the link below:
  2. {{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb36=uid token=token %}
复制代码
The same template context is used for subject template. Subject must be single line plain text string.

password_reset_done(request[, template_name])

The page shown after a user has been emailed a link to reset their password. This view is called by default if thepassword_reset() view doesn't have an explicit post_reset_redirect URL set.

URL name: password_reset_done

Optional arguments:

template_name: The full name of a template to use. Defaults to registration/password_reset_done.html if not supplied.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
49#
 楼主| 发表于 2012-12-21 22:30 | 只看该作者
password_reset_confirm(request[, uidb36, token, template_name, token_generator, set_password_form,post_reset_redirect])
Presents a form for entering a new password.

URL name: password_reset_confirm

Optional arguments:

uidb36: The user's id encoded in base 36. Defaults to None.

token: Token to check that the password is valid. Defaults to None.

template_name: The full name of a template to display the confirm password view. Default value isregistration/password_reset_confirm.html.

token_generator: Instance of the class to check the password. This will default to default_token_generator, it's an instance of django.contrib.auth.tokens.PasswordResetTokenGenerator.

set_password_form: Form that will be used to set the password. Defaults to SetPasswordForm

post_reset_redirect: URL to redirect after the password reset done. Defaults to None.

Template context:

form: The form (see set_password_form above) for setting the new user's password.

validlink: Boolean, True if the link (combination of uidb36 and token) is valid or unused yet.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
50#
 楼主| 发表于 2012-12-21 22:30 | 只看该作者
password_reset_complete(request[, template_name])

Presents a view which informs the user that the password has been successfully changed.

URL name: password_reset_complete

Optional arguments:

template_name: The full name of a template to display the view. Defaults to registration/password_reset_complete.html.

Helper functions

redirect_to_login(next[, login_url, redirect_field_name])

Redirects to the login page, and then back to another URL after a successful login.

Required arguments:

next: The URL to redirect to after a successful login.

Optional arguments:

login_url: The URL of the login page to redirect to. Defaults to settings.LOGIN_URL if not supplied.

redirect_field_name: The name of a GET field containing the URL to redirect to after log out. Overrides next if the given GETparameter is passed.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表