Ticket #8274: authentication_form.diff
File authentication_form.diff, 8.8 KB (added by , 16 years ago) |
---|
-
django/contrib/auth/views.py
14 14 from django.contrib.auth.models import User 15 15 from django.views.decorators.cache import never_cache 16 16 17 def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME): 17 def login(request, template_name='registration/login.html', 18 redirect_field_name=REDIRECT_FIELD_NAME, 19 authentication_form=AuthenticationForm): 18 20 "Displays the login form and handles the login action." 19 21 redirect_to = request.REQUEST.get(redirect_field_name, '') 20 22 if request.method == "POST": 21 form = AuthenticationForm(data=request.POST)23 form = authentication_form(data=request.POST) 22 24 if form.is_valid(): 23 25 # Light security check -- make sure redirect_to isn't garbage. 24 26 if not redirect_to or '//' in redirect_to or ' ' in redirect_to: … … 29 31 request.session.delete_test_cookie() 30 32 return HttpResponseRedirect(redirect_to) 31 33 else: 32 form = AuthenticationForm(request)34 form = authentication_form(request) 33 35 request.session.set_test_cookie() 34 36 if Site._meta.installed: 35 37 current_site = Site.objects.get_current() … … 67 69 # 4 views for password reset: 68 70 # - password_reset sends the mail 69 71 # - password_reset_done shows a success message for the above 70 # - password_reset_confirm checks the link the user clicked and 72 # - password_reset_confirm checks the link the user clicked and 71 73 # prompts for a new password 72 74 # - password_reset_complete shows a success message for the above 73 75 … … 130 132 else: 131 133 context_instance['validlink'] = False 132 134 form = None 133 context_instance['form'] = form 135 context_instance['form'] = form 134 136 return render_to_response(template_name, context_instance=context_instance) 135 137 136 138 def password_reset_complete(request, template_name='registration/password_reset_complete.html'): … … 138 140 {'login_url': settings.LOGIN_URL})) 139 141 140 142 def password_change(request, template_name='registration/password_change_form.html', 141 post_change_redirect=None ):143 post_change_redirect=None, password_change_form=PasswordChangeForm): 142 144 if post_change_redirect is None: 143 145 post_change_redirect = reverse('django.contrib.auth.views.password_change_done') 144 146 if request.method == "POST": 145 form = PasswordChangeForm(request.user,request.POST)147 form = password_change_form(user=request.user, data=request.POST) 146 148 if form.is_valid(): 147 149 form.save() 148 150 return HttpResponseRedirect(post_change_redirect) 149 151 else: 150 form = PasswordChangeForm(request.user)152 form = password_change_form(user=request.user) 151 153 return render_to_response(template_name, { 152 154 'form': form, 153 155 }, context_instance=RequestContext(request)) -
docs/topics/auth.txt
246 246 .. method:: models.UserManager.create_user(username, email, password=None) 247 247 248 248 Creates, saves and returns a :class:`~django.contrib.auth.models.User`. 249 The :attr:`~django.contrib.auth.models.User.username`, 250 :attr:`~django.contrib.auth.models.User.email` and 249 The :attr:`~django.contrib.auth.models.User.username`, 250 :attr:`~django.contrib.auth.models.User.email` and 251 251 :attr:`~django.contrib.auth.models.User.password` are set as given, and the 252 252 :class:`~django.contrib.auth.models.User` gets ``is_active=True``. 253 253 … … 351 351 ``False`` instead of ``True``. 352 352 * :meth:`~django.contrib.auth.models.User.has_perm()` always returns ``False``. 353 353 * :meth:`~django.contrib.auth.models.User.set_password()`, 354 :meth:`~django.contrib.auth.models.User.check_password()`, 355 :meth:`~django.contrib.auth.models.User.save()`, 354 :meth:`~django.contrib.auth.models.User.check_password()`, 355 :meth:`~django.contrib.auth.models.User.save()`, 356 356 :meth:`~django.contrib.auth.models.User.delete()`, 357 :meth:`~django.contrib.auth.models.User.set_groups()` and 358 :meth:`~django.contrib.auth.models.User.set_permissions()` raise 357 :meth:`~django.contrib.auth.models.User.set_groups()` and 358 :meth:`~django.contrib.auth.models.User.set_permissions()` raise 359 359 :exc:`NotImplementedError`. 360 360 361 361 In practice, you probably won't need to use … … 523 523 :func:`~django.contrib.auth.authenticate()` 524 524 sets an attribute on the :class:`~django.contrib.auth.models.User` noting 525 525 which authentication backend successfully authenticated that user (see 526 the `backends documentation`_ for details), and this information is527 neededlater during the login process.526 :ref:`authentication-backends` for details), and this information is needed 527 later during the login process. 528 528 529 .. _backends documentation: #other-authentication-sources530 531 529 Manually checking a user's password 532 530 ----------------------------------- 533 531 … … 683 681 :class:`~django.contrib.sites.models.Site``, according to the 684 682 :setting:`SITE_ID` setting. If you're using the Django development version 685 683 and you don't have the site framework installed, this will be set to the 686 value of ``request.META['SERVER_NAME']``. For more on sites, see 684 value of ``request.META['SERVER_NAME']``. For more on sites, see 687 685 :ref:`ref-contrib-sites`. 688 686 689 687 If you'd prefer not to call the template :file:`registration/login.html`, … … 717 715 718 716 {% endblock %} 719 717 718 **New in Django development version** 719 720 If you are using alternate authentication (see 721 :ref:`authentication-backends`) you can pass a custom authentication form 722 to the login view via the ``authentication_form`` parameter. This form must 723 accept a ``request`` keyword argument in its ``__init__`` method, and 724 provide a ``get_user`` argument which returns the authenticated user object 725 (this method is only ever called after successful form validation). 726 720 727 .. _forms documentation: ../forms/ 721 728 .. _site framework docs: ../sites/ 722 729 … … 760 767 displaying the password change form. This will default to 761 768 :file:`registration/password_change_form.html` if not supplied. 762 769 770 * ``post_change_redirect``: The URL which the user will be redirected 771 to after a successful password change (defaults to the reverse of the 772 :func:`django.contrib.auth.views.password_change_done` view). 773 774 * ``password_change_form``: A custom "change password" form which must 775 accept a ``user`` keyword argument. The form is responsible for 776 actually changing the user's password. 777 (**New in Django development version**) 778 763 779 **Template context:** 764 780 765 781 * ``form``: The password change form. … … 1029 1045 fields: 1030 1046 1031 1047 .. attribute:: models.Permission.name 1032 1048 1033 1049 Required. 50 characters or fewer. Example: ``'Can vote'``. 1034 1050 1035 1051 .. attribute:: models.Permission.content_type … … 1038 1054 which contains a record for each installed Django model. 1039 1055 1040 1056 .. attribute:: models.Permission.codename 1041 1057 1042 1058 Required. 100 characters or fewer. Example: ``'can_vote'``. 1043 1059 1044 1060 Methods … … 1061 1077 :setting:`TEMPLATE_CONTEXT_PROCESSORS` setting contains 1062 1078 ``"django.core.context_processors.auth"``, which is default. For more, see 1063 1079 the :ref:`RequestContext docs <subclassing-context-requestcontext>`. 1064 1080 1065 1081 Users 1066 1082 ----- 1067 1083 … … 1091 1107 1092 1108 {{ perms.foo }} 1093 1109 1094 Two-level-attribute lookup is a proxy to 1110 Two-level-attribute lookup is a proxy to 1095 1111 :meth:`User.has_perm <django.contrib.auth.models.User.has_perm>`. This example 1096 1112 would display ``True`` if the logged-in user had the permission 1097 1113 ``foo.can_vote``:: … … 1145 1161 1146 1162 To create a new message, use 1147 1163 ``user_obj.message_set.create(message='message_text')``. 1148 1164 1149 1165 To retrieve/delete messages, use 1150 1166 :meth:`user_obj.get_and_delete_messages() <django.contrib.auth.models.User.get_and_delete_messages>`, 1151 1167 which returns a list of ``Message`` objects in the user's queue (if any) … … 1305 1321 1306 1322 The user model will delegate permission lookup functions 1307 1323 (:meth:`~django.contrib.auth.models.User.get_group_permissions()`, 1308 :meth:`~django.contrib.auth.models.User.get_all_permissions()`, 1324 :meth:`~django.contrib.auth.models.User.get_all_permissions()`, 1309 1325 :meth:`~django.contrib.auth.models.User.has_perm()`, and 1310 1326 :meth:`~django.contrib.auth.models.User.has_module_perms()`) to any 1311 1327 authentication backend that implements these functions.