login and logout

This commit is contained in:
Sharad Ahlawat 2020-03-10 09:40:19 -07:00
parent 4681cef584
commit d1ac07a066
4 changed files with 45 additions and 6 deletions

View File

@ -1,4 +1,21 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {% 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 %} {% endblock %}

View File

@ -20,8 +20,18 @@ def signup(request):
def login(request): def login(request):
return render(request, 'accounts/login.html') 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): def logout(request):
return render(request, '/accounts/login.html') if request.method == 'POST':
auth.logout(request)
return redirect('home')

View File

@ -50,8 +50,16 @@
</button> </button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup"> <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav ml-auto"> <div class="navbar-nav ml-auto">
<a class="nav-item nav-link" href="{% url 'home' %}">Sign Up</a> {% if user.is_authenticated %}
<a class="nav-item nav-link" href="{% url 'home' %}">Login</a> <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> </div>
</div> </div>

View File

@ -1,4 +1,8 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {% block content %}
HI {% if error %}
{{ error }}
<br/>
<br/>
{% endif %}
{% endblock %} {% endblock %}