login and logout
This commit is contained in:
parent
4681cef584
commit
d1ac07a066
@ -1,4 +1,21 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
HI
|
||||
{% if error %}
|
||||
{{ error }}
|
||||
<br />
|
||||
<br />
|
||||
{% endif %}
|
||||
<h1>Login</h1>
|
||||
<form method="POST" action="{% url 'login' %}">
|
||||
{% csrf_token %}
|
||||
Username:
|
||||
<br/>
|
||||
<input type="text" name="username"/>
|
||||
<br/>
|
||||
Password:
|
||||
<br/>
|
||||
<input type="password" name="password"/>
|
||||
<br/><br />
|
||||
<input type="submit" value="Login" class="btn btn-primary">
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
@ -20,8 +20,18 @@ def signup(request):
|
||||
|
||||
|
||||
def login(request):
|
||||
if request.method == 'POST':
|
||||
user = auth.authenticate(username=request.POST['username'], password=request.POST['password'])
|
||||
if user is not None:
|
||||
auth.login(request, user)
|
||||
return redirect('home')
|
||||
else:
|
||||
return render(request, 'accounts/login.html', {'error': 'username or password incorrect'})
|
||||
else:
|
||||
return render(request, 'accounts/login.html')
|
||||
|
||||
|
||||
def logout(request):
|
||||
return render(request, '/accounts/login.html')
|
||||
if request.method == 'POST':
|
||||
auth.logout(request)
|
||||
return redirect('home')
|
||||
|
@ -50,8 +50,16 @@
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
|
||||
<div class="navbar-nav ml-auto">
|
||||
<a class="nav-item nav-link" href="{% url 'home' %}">Sign Up</a>
|
||||
<a class="nav-item nav-link" href="{% url 'home' %}">Login</a>
|
||||
{% if user.is_authenticated %}
|
||||
<a class="nav-item nav-link" href="javascript:{document.getElementById('logout').submit()}">Logout</a>
|
||||
<form id="logout" method="POST" action="{% url 'logout' %}">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" />
|
||||
</form>
|
||||
{% else %}
|
||||
<a class="nav-item nav-link" href="{% url 'signup' %}">Sign Up</a>
|
||||
<a class="nav-item nav-link" href="{% url 'login' %}">Login</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -1,4 +1,8 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
HI
|
||||
{% if error %}
|
||||
{{ error }}
|
||||
<br/>
|
||||
<br/>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
Loading…
Reference in New Issue
Block a user