Initial plant reference and basic login
This commit is contained in:
parent
a2dba58fe4
commit
cada3c7357
31 changed files with 637 additions and 0 deletions
72
planner/views.py
Normal file
72
planner/views.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
from django.shortcuts import render, redirect
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import login, authenticate, logout
|
||||
from .forms import LoginForm, RegisterForm
|
||||
import logging
|
||||
|
||||
def index(request):
|
||||
# from django.shortcuts import render
|
||||
# context = { "latest_plant_list": latest_plant_list }
|
||||
# return render(request, "planterteque/index.html", context)
|
||||
#template = loader.get_template("planner/index.html")
|
||||
#context = { }
|
||||
#return HttpResponse( template.render( context, request ) )
|
||||
return render(request, 'planner/index.html')
|
||||
|
||||
## Form action to log in
|
||||
def sign_in(request):
|
||||
if request.user.is_authenticated and request.method == 'GET':
|
||||
return redirect('planner:index')
|
||||
elif request.method == 'GET':
|
||||
form = LoginForm()
|
||||
return render(request, 'planner/login.html', {'form': form})
|
||||
elif request.method == 'POST':
|
||||
form = LoginForm(request.POST)
|
||||
|
||||
if form.is_valid():
|
||||
username = form.cleaned_data['username']
|
||||
password = form.cleaned_data['password']
|
||||
user = authenticate(request,username=username,password=password)
|
||||
if user:
|
||||
logging.warning('User %s is valid', username.title())
|
||||
login(request, user)
|
||||
messages.success(request,f'Hi {username.title()}, welcome back!')
|
||||
return redirect('planner:index')
|
||||
#return index(request)
|
||||
else:
|
||||
logging.warning('User login failed')
|
||||
else:
|
||||
logging.warning('User form invalid')
|
||||
|
||||
# form is not valid or user is not authenticated
|
||||
messages.error(request,f'Invalid username or password')
|
||||
return render(request,'planner/login.html',{'form': form})
|
||||
return redirect('planner:index')
|
||||
|
||||
## Form action to log out
|
||||
def sign_out(request):
|
||||
if (request.user.is_authenticated):
|
||||
logout(request)
|
||||
messages.success(request,f'You have been logged out.')
|
||||
return render(request, 'planner/index.html', {})
|
||||
return redirect('planner:index')
|
||||
|
||||
def sign_up(request):
|
||||
if request.method == 'GET':
|
||||
form = RegisterForm()
|
||||
return render(request, 'planner/register.html', { 'form': form})
|
||||
if request.method == 'POST':
|
||||
form = RegisterForm(request.POST)
|
||||
if form.is_valid():
|
||||
user = form.save(commit=False)
|
||||
user.username = user.username.lower()
|
||||
user.save()
|
||||
messages.success(request, 'You have signed up successfully.')
|
||||
login(request, user)
|
||||
return redirect('planner:index')
|
||||
else:
|
||||
return render(request, 'planner/register.html', {'form': form})
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue