Django

Code

Changeset 2954

Show
Ignore:
Timestamp:
05/21/06 21:46:55 (2 years ago)
Author:
adrian
Message:

Fixed #849 -- Improved login_required view decorator to save query-string parameters. Also added documentation on the django.contrib.auth.views.login view to docs/authentication.txt

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/auth/decorators.py

    r2809 r2954  
    11from django.contrib.auth import LOGIN_URL, REDIRECT_FIELD_NAME 
    22from django.http import HttpResponseRedirect 
     3from urllib import quote 
    34 
    45def user_passes_test(test_func, login_url=LOGIN_URL): 
     
    1213            if test_func(request.user): 
    1314                return view_func(request, *args, **kwargs) 
    14             return HttpResponseRedirect('%s?%s=%s' % (login_url, REDIRECT_FIELD_NAME, request.path)) 
     15            return HttpResponseRedirect('%s?%s=%s' % (login_url, REDIRECT_FIELD_NAME, quote(request.get_full_path()))) 
    1516 
    1617        return _checklogin 
  • django/trunk/docs/authentication.txt

    r2908 r2954  
    331331      free to assume the user is logged in. 
    332332 
     333Note that you'll need to map the appropriate Django view to ``/accounts/login/``. 
     334To do this, add the following line to your URLconf:: 
     335 
     336    (r'^accounts/login/$', 'django.contrib.auth.views.login'), 
     337 
     338Here's what ``django.contrib.auth.views.login`` does:: 
     339 
     340    * If called via ``GET``, it displays a login form that POSTs to the same 
     341      URL. More on this in a bit. 
     342 
     343    * If called via ``POST``, it tries to log the user in. If login is 
     344      successful, the view redirects to the URL specified in ``next``. If 
     345      ``next`` isn't provided, it redirects to ``/accounts/profile/`` (which is 
     346      currently hard-coded). If login isn't successful, it redisplays the login 
     347      form. 
     348 
     349It's your responsibility to provide the login form in a template called 
     350``registration/login.html``. This template gets passed three template context 
     351variables: 
     352 
     353    * ``form``: A ``FormWrapper`` object representing the login form. See the 
     354      `forms documentation`_ for more on ``FormWrapper`` objects. 
     355    * ``next``: The URL to redirect to after successful login. This may contain 
     356      a query string, too. 
     357    * ``site_name``: The name of the current ``Site``, according to the 
     358      ``SITE_ID`` setting. 
     359 
     360Here's a sample ``registration/login.html`` template you can use as a starting 
     361point. It assumes you have a ``base.html`` template that defines a ``content`` 
     362block:: 
     363 
     364    {% extends "base.html" %} 
     365 
     366    {% block content %} 
     367 
     368    {% if form.has_errors %} 
     369    <p>Your username and password didn't match. Please try again.</p> 
     370    {% endif %} 
     371 
     372    <form method="post" action="."> 
     373    <table> 
     374    <tr><td><label for="id_username">Username:</label></td><td>{{ form.username }}</td></tr> 
     375    <tr><td><label for="id_password">Password:</label></td><td>{{ form.password }}</td></tr> 
     376    </table> 
     377 
     378    <input type="submit" value="login" /> 
     379    <input type="hidden" name="next" value="{{ next }}" /> 
     380    </form> 
     381 
     382    {% endblock %} 
     383 
     384.. _forms documentation: http://www.djangoproject.com/documentation/forms/ 
     385 
    333386Limiting access to logged-in users that pass a test 
    334387---------------------------------------------------