| | 333 | Note that you'll need to map the appropriate Django view to ``/accounts/login/``. |
|---|
| | 334 | To do this, add the following line to your URLconf:: |
|---|
| | 335 | |
|---|
| | 336 | (r'^accounts/login/$', 'django.contrib.auth.views.login'), |
|---|
| | 337 | |
|---|
| | 338 | Here'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 | |
|---|
| | 349 | It's your responsibility to provide the login form in a template called |
|---|
| | 350 | ``registration/login.html``. This template gets passed three template context |
|---|
| | 351 | variables: |
|---|
| | 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 | |
|---|
| | 360 | Here's a sample ``registration/login.html`` template you can use as a starting |
|---|
| | 361 | point. It assumes you have a ``base.html`` template that defines a ``content`` |
|---|
| | 362 | block:: |
|---|
| | 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 | |
|---|