Refactor login and logout with built-in views

This commit is contained in:
Skia 2015-11-25 16:20:28 +01:00
parent b237cdbaae
commit 04bbf0db5b
5 changed files with 41 additions and 27 deletions

View File

@ -10,7 +10,7 @@
{% block header %} {% block header %}
{% if user.is_authenticated %}Hello, {{ user.username }}!{% endif %} {% if user.is_authenticated %}Hello, {{ user.username }}!{% endif %}
<ul> <ul>
<li><a href="{% url 'core:register' %}">Register</a></li> <li><a href="">Register</a></li>
<li><a href="{% url 'core:login' %}">Login</a></li> <li><a href="{% url 'core:login' %}">Login</a></li>
<li><a href="{% url 'core:logout' %}">Logout</a></li> <li><a href="{% url 'core:logout' %}">Logout</a></li>
<li><a href="{% url 'core:user_list' %}">Users</a></li> <li><a href="{% url 'core:user_list' %}">Users</a></li>

View File

@ -1,14 +1,38 @@
{% extends "core/base.html" %} {% extends "core/base.html" %}
{% block title %}{{ title }}{% endblock %}
{% block content %} {% block content %}
<h1>{{ title }}</h1>
<form action="{% url 'core:login' %}" method="post"> {% if form.errors %}
{% csrf_token %} <p>Your username and password didn't match. Please try again.</p>
{{ form }} {% endif %}
<p><input type="submit" value="Login!" /></p>
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'core:login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form> </form>
{% endblock %}
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'core:password_reset' %}">Lost password?</a></p>
{% endblock %}

View File

@ -1,8 +1,9 @@
from django.conf.urls import url from django.conf.urls import url, include
from core.views import * from core.views import *
urlpatterns = [ urlpatterns = [
url('^', include('django.contrib.auth.urls')),
url(r'^$', index, name='index'), url(r'^$', index, name='index'),
url(r'^login$', login, name='login'), url(r'^login$', login, name='login'),
url(r'^logout$', logout, name='logout'), url(r'^logout$', logout, name='logout'),

View File

@ -1,7 +1,9 @@
# This file contains all the views that concern the user model # This file contains all the views that concern the user model
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import logout as auth_logout from django.contrib.auth import logout as auth_logout
from django.contrib.auth import views
from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.forms import PasswordChangeForm
from django.core.urlresolvers import reverse
import logging import logging
from core.views.forms import RegisteringForm, LoginForm, UserEditForm from core.views.forms import RegisteringForm, LoginForm, UserEditForm
@ -31,28 +33,13 @@ def login(request):
Needs to be improve with correct handling of form exceptions Needs to be improve with correct handling of form exceptions
""" """
context = {'title': 'Login'} return views.login(request, template_name="core/login.html")
if request.method == 'POST':
try:
form = LoginForm(request)
form.login()
context['tests'] = 'LOGIN_OK'
return render(request, 'core/index.html', context)
except Exception as e:
logging.debug(e)
context['error'] = "Login failed"
context['tests'] = 'LOGIN_FAIL'
else:
form = LoginForm()
context['form'] = form.as_p()
return render(request, "core/login.html", context)
def logout(request): def logout(request):
""" """
The logout view The logout view
""" """
auth_logout(request) return views.logout_then_login(request)
return redirect('core:index')
def user(request, user_id=None): def user(request, user_id=None):
""" """

View File

@ -102,3 +102,5 @@ USE_TZ = True
STATIC_URL = '/static/' STATIC_URL = '/static/'
AUTH_USER_MODEL = 'core.User' AUTH_USER_MODEL = 'core.User'
LOGIN_URL = '/login'
LOGIN_REDIRECT_URL = '/'