add login form

adam
aundus 2021-02-19 14:41:57 -05:00
parent afe81a2e91
commit c624356b87
4 changed files with 51 additions and 1 deletions

View File

@ -14,6 +14,13 @@ class BookSearchForm(forms.Form):
return str(searchTerm) != ''
class LoginForm(forms.Form):
username = forms.CharField(label='Username', max_length=64)
password = forms.CharField(label='Password', max_length=64)
def is_valid():
return str(username) != '' and str(password) != ''

View File

@ -0,0 +1,13 @@
{% extends "generic_template.html" %}
{% block content %}
<form action="/login" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Login">
</form>
<p>{{message}}</p>
{% endblock %}

View File

@ -1,10 +1,11 @@
from .models import Book, Bookshelf, BookshelfToBook
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate
from .forms import NewBookForm, BookSearchForm
from .forms import NewBookForm, BookSearchForm, LoginForm
from django.views import generic
# class BookListView(generic.ListView):
@ -154,3 +155,31 @@ def get_name(request):
form = NewBookForm()
return render(request, 'name.html', {'form': form})
def login(request):
context = {
"message": ""
}
if request.method == 'POST':
form = (request.POST)
# print(form)
# check whether it's valid:
if form.get('username') != None and form.get('password') != None:
user = authenticate(username=form.get('username'), password=form.get('password'))
print(user)
if user is not None:
# A backend authenticated the credentials
return redirect("/bookshelves")
else:
# No backend authenticated the credentials
context["message"] = 'Invalid Login'
context['form'] = LoginForm()
return render(request, 'login.html', context=context)
# redirect to a new URL:
# if a GET (or any other method) we'll create a blank form
else:
form = LoginForm()
context['form'] = form
return render(request, 'login.html', context=context)

View File

@ -24,6 +24,7 @@ urlpatterns = [
path('admin/', admin.site.urls),
path('bookshelves', views.bookshelfList, name='detail'),
path('books', views.searchBooks, name='detail'),
path('login', views.login, name='detail'),
path('books/<int:bookId>', views.bookshelvesOfBook, name='detail'),
path('bookshelves/<int:bookshelfId>/search', views.searchBooksToAdd, name='detail'),
path('bookshelves/<int:bookshelfId>', views.booksInBookshelf, name='detail')