Ticket #3185: login_url.patch

File login_url.patch, 10.4 KB (added by Collin Grady <cgrady@…>, 17 years ago)
  • django/conf/global_settings.py

     
    312312
    313313AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)
    314314
     315LOGIN_URL = '/accounts/login/'
     316
     317LOGOUT_URL = '/accounts/logout/'
     318
     319POSTLOGIN_REDIRECT_URL = '/accounts/profile/'
     320
    315321###########
    316322# TESTING #
    317323###########
  • django/contrib/comments/templatetags/comments.py

     
    2525        self.is_public = is_public
    2626
    2727    def render(self, context):
     28        from django.conf import settings
    2829        from django.utils.text import normalize_newlines
    2930        import base64
    3031        context.push()
     
    6465            if self.rating_options:
    6566                context['rating_range'], context['rating_choices'] = Comment.objects.get_rating_options(self.rating_options)
    6667            context['hash'] = Comment.objects.get_security_hash(context['options'], context['photo_options'], context['rating_options'], context['target'])
     68            context['logout_url'] = settings.LOGOUT_URL
    6769            default_form = loader.get_template(COMMENT_FORM)
    6870        output = default_form.render(context)
    6971        context.pop()
  • django/contrib/comments/templates/comments/form.html

     
    33<form {% if photos_optional or photos_required %}enctype="multipart/form-data" {% endif %}action="/comments/post/" method="post">
    44
    55{% if user.is_authenticated %}
    6 <p>{% trans "Username:" %} <strong>{{ user.username }}</strong> (<a href="/accounts/logout/">{% trans "Log out" %}</a>)</p>
     6<p>{% trans "Username:" %} <strong>{{ user.username }}</strong> (<a href="{{ logout_url }}">{% trans "Log out" %}</a>)</p>
    77{% else %}
    88<p><label for="id_username">{% trans "Username:" %}</label> <input type="text" name="username" id="id_username" /><br />{% trans "Password:" %} <input type="password" name="password" id="id_password" /> (<a href="/accounts/password_reset/">{% trans "Forgotten your password?" %}</a>)</p>
    99{% endif %}
  • django/contrib/auth/views.py

     
    66from django.contrib.sites.models import Site
    77from django.http import HttpResponseRedirect
    88from django.contrib.auth.decorators import login_required
    9 from django.contrib.auth import LOGIN_URL, REDIRECT_FIELD_NAME
     9from django.contrib.auth import REDIRECT_FIELD_NAME
    1010
    1111def login(request, template_name='registration/login.html'):
    1212    "Displays the login form and handles the login action."
     
    1717        if not errors:
    1818            # Light security check -- make sure redirect_to isn't garbage.
    1919            if not redirect_to or '://' in redirect_to or ' ' in redirect_to:
    20                 redirect_to = '/accounts/profile/'
     20                from django.conf import settings
     21                redirect_to = settings.POSTLOGIN_REDIRECT_URL
    2122            from django.contrib.auth import login
    2223            login(request, manipulator.get_user())
    2324            request.session.delete_test_cookie()
     
    4142        # Redirect to this page until the session has been cleared.
    4243        return HttpResponseRedirect(next_page or request.path)
    4344
    44 def logout_then_login(request, login_url=LOGIN_URL):
     45def logout_then_login(request, login_url=None):
    4546    "Logs out the user if he is logged in. Then redirects to the log-in page."
     47    if not login_url:
     48        from django.conf import settings
     49        login_url = settings.LOGIN_URL
    4650    return logout(request, login_url)
    4751
    48 def redirect_to_login(next, login_url=LOGIN_URL):
     52def redirect_to_login(next, login_url=None):
    4953    "Redirects the user to the login page, passing the given 'next' page"
     54    if not login_url:
     55        from django.conf import settings
     56        login_url = settings.LOGIN_URL
    5057    return HttpResponseRedirect('%s?%s=%s' % (login_url, REDIRECT_FIELD_NAME, next))
    5158
    5259def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html',
  • django/contrib/auth/__init__.py

     
    22
    33SESSION_KEY = '_auth_user_id'
    44BACKEND_SESSION_KEY = '_auth_user_backend'
    5 LOGIN_URL = '/accounts/login/'
    65REDIRECT_FIELD_NAME = 'next'
    76
    87def load_backend(path):
  • django/contrib/auth/decorators.py

     
    1 from django.contrib.auth import LOGIN_URL, REDIRECT_FIELD_NAME
     1from django.contrib.auth import REDIRECT_FIELD_NAME
    22from django.http import HttpResponseRedirect
    33from urllib import quote
    44
    5 def user_passes_test(test_func, login_url=LOGIN_URL):
     5def user_passes_test(test_func, login_url=None):
    66    """
    77    Decorator for views that checks that the user passes the given test,
    88    redirecting to the log-in page if necessary. The test should be a callable
    99    that takes the user object and returns True if the user passes.
    1010    """
     11    if not login_url:
     12        from django.conf import settings
     13        login_url = settings.LOGIN_URL
    1114    def _dec(view_func):
    1215        def _checklogin(request, *args, **kwargs):
    1316            if test_func(request.user):
     
    2730    """
    2831    )
    2932
    30 def permission_required(perm, login_url=LOGIN_URL):
     33def permission_required(perm, login_url=None):
    3134    """
    3235    Decorator for views that checks whether a user has a particular permission
    3336    enabled, redirecting to the log-in page if necessary.
  • docs/settings.txt

     
    562562you'll have to remember to wrap the languages in the *real* ``gettext()`` in
    563563any code that uses ``LANGUAGES`` at runtime.
    564564
     565LOGIN_URL
     566---------
     567
     568Default: ``'/accounts/login/'``
     569
     570The URL where requests are redirected for login, specially when using the
     571`@login_required`_ decorator.
     572
     573LOGOUT_URL
     574----------
     575
     576Default: ``'/accounts/logout/'``
     577
     578LOGIN_URL counterpart.
     579
    565580MANAGERS
    566581--------
    567582
     
    620635See `allowed date format strings`_. See also DATE_FORMAT, DATETIME_FORMAT,
    621636TIME_FORMAT and YEAR_MONTH_FORMAT.
    622637
     638POSTLOGIN_REDIRECT_URL
     639----------------------
     640
     641Default: ``'/accounts/profile/'``
     642
     643The URL where requests are redirected after login when the ``contrib.auth.login``
     644view gets no ``next`` parameter.
     645i.e.: When the `@login_required`_ decorator is called.
     646
    623647PREPEND_WWW
    624648-----------
    625649
     
    10121036It boils down to this: Use exactly one of either ``configure()`` or
    10131037``DJANGO_SETTINGS_MODULE``. Not both, and not neither.
    10141038
     1039.. _@login_required: ../authentication/#the-login-required-decorator
     1040
    10151041Error reporting via e-mail
    10161042==========================
    10171043
  • docs/authentication.txt

     
    387387
    388388``login_required`` does the following:
    389389
    390     * If the user isn't logged in, redirect to ``/accounts/login/``, passing
    391       the current absolute URL in the query string as ``next``. For example:
     390    * If the user isn't logged in, redirect to ``settings.LOGIN_URL``
     391      (``/accounts/login/`` by default), passing the current absolute URL
     392      in the query string as ``next``. For example:
    392393      ``/accounts/login/?next=/polls/3/``.
    393394    * If the user is logged in, execute the view normally. The view code is
    394395      free to assume the user is logged in.
    395396
    396 Note that you'll need to map the appropriate Django view to ``/accounts/login/``.
    397 To do this, add the following line to your URLconf::
     397Note that you'll need to map the appropriate Django view to ``settings.LOGIN_URL``.
     398For example, using the defaults, add the following line to your URLconf::
    398399
    399400    (r'^accounts/login/$', 'django.contrib.auth.views.login'),
    400401
     
    405406
    406407    * If called via ``POST``, it tries to log the user in. If login is
    407408      successful, the view redirects to the URL specified in ``next``. If
    408       ``next`` isn't provided, it redirects to ``/accounts/profile/`` (which is
    409       currently hard-coded). If login isn't successful, it redisplays the login
    410       form.
     409      ``next`` isn't provided, it redirects to ``settings.POSTLOGIN_REDIRECT_URL``
     410      which defaults to ``/accounts/profile/``. If login isn't successful, it
     411      redisplays the login form.
    411412
    412413It's your responsibility to provide the login form in a template called
    413414``registration/login.html`` by default. This template gets passed three
     
    487488**Optional arguments:**
    488489
    489490    * ``login_url``: The URL of the login page to redirect to. This
    490       will default to ``/accounts/login/`` if not supplied.
     491      will default to ``settings.LOGIN_URL`` if not supplied.
    491492
    492493``django.contrib.auth.views.password_change``
    493494~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    569570**Optional arguments:**
    570571
    571572    * ``login_url``: The URL of the login page to redirect to. This
    572       will default to ``/accounts/login/`` if not supplied.
     573      will default to ``settings.LOGIN_URL`` if not supplied.
    573574
    574575Built-in manipulators
    575576---------------------
     
    636637is not anonymous.
    637638
    638639``user_passes_test()`` takes an optional ``login_url`` argument, which lets you
    639 specify the URL for your login page (``/accounts/login/`` by default).
     640specify the URL for your login page (``settings.LOGIN_URL`` by default).
    640641
    641642Example in Python 2.3 syntax::
    642643
     
    680681    my_view = permission_required('polls.can_vote', login_url='/loginpage/')(my_view)
    681682
    682683As in the ``login_required`` decorator, ``login_url`` defaults to
    683 ``'/accounts/login/'``.
     684``settings.LOGIN_URL``.
    684685
    685686Limiting access to generic views
    686687--------------------------------
Back to Top