Add: code comments
This commit is contained in:
parent
a210588452
commit
e2e4994954
1 changed files with 29 additions and 27 deletions
|
@ -2,65 +2,67 @@
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
from django.contrib.auth import authenticate, login
|
from django.contrib.auth import authenticate, login
|
||||||
|
|
||||||
#from django.contrib.auth.models import User
|
from apps.user.models import UserProfile # Importing the UserProfile model
|
||||||
from apps.user.models import UserProfile
|
|
||||||
|
|
||||||
from .forms import LoginForm, SignUpForm
|
from .forms import LoginForm, SignUpForm # Importing form classes for login and signup
|
||||||
|
|
||||||
|
|
||||||
|
# View function for user login
|
||||||
def login_view(request):
|
def login_view(request):
|
||||||
|
# Check if the user is already authenticated, if so, redirect them to the home page
|
||||||
if request.user.is_authenticated:
|
if request.user.is_authenticated:
|
||||||
# User is already logged in, redirect them to the home page
|
next_page = request.GET.get('next', '/') # Get the 'next' parameter from the URL, default to '/'
|
||||||
next_page = request.GET.get('next', '/')
|
|
||||||
return redirect(next_page)
|
return redirect(next_page)
|
||||||
form = LoginForm(request.POST or None)
|
|
||||||
|
|
||||||
msg = None
|
form = LoginForm(request.POST or None) # Create a login form instance
|
||||||
|
|
||||||
|
msg = None # Initialize a message variable
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
username = form.cleaned_data.get("username")
|
username = form.cleaned_data.get("username")
|
||||||
password = form.cleaned_data.get("password")
|
password = form.cleaned_data.get("password")
|
||||||
|
|
||||||
|
# Authenticate user using the provided username and password
|
||||||
user = authenticate(username=username, password=password)
|
user = authenticate(username=username, password=password)
|
||||||
|
|
||||||
if user is not None:
|
if user is not None:
|
||||||
login(request, user)
|
login(request, user) # Log in the authenticated user
|
||||||
next_page = request.GET.get('next', '/')
|
next_page = request.GET.get('next', '/') # Get the 'next' parameter from the URL
|
||||||
return redirect(next_page)
|
return redirect(next_page) # Redirect to the 'next' page after successful login
|
||||||
else:
|
else:
|
||||||
msg = 'Invalid credentials'
|
msg = 'Invalid credentials' # Set error message for invalid credentials
|
||||||
else:
|
else:
|
||||||
msg = 'Error validating the form'
|
msg = 'Error validating the form' # Set error message for form validation error
|
||||||
|
|
||||||
return render(request, "accounts/login.html", {"form": form, "msg": msg})
|
return render(request, "accounts/login.html", {"form": form, "msg": msg})
|
||||||
|
|
||||||
|
|
||||||
|
# View function for user registration
|
||||||
def register_user(request):
|
def register_user(request):
|
||||||
msg = None
|
msg = None # Initialize a message variable
|
||||||
success = False
|
success = False # Initialize a success flag
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
form = SignUpForm(request.POST)
|
form = SignUpForm(request.POST) # Create a signup form instance
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save()
|
form.save() # Save the user details from the form
|
||||||
username = form.cleaned_data.get("username")
|
username = form.cleaned_data.get("username")
|
||||||
raw_password = form.cleaned_data.get("password1")
|
raw_password = form.cleaned_data.get("password1")
|
||||||
|
|
||||||
|
# Authenticate the newly registered user
|
||||||
user = authenticate(username=username, password=raw_password)
|
user = authenticate(username=username, password=raw_password)
|
||||||
|
|
||||||
#user = User.objects.create_user(username=username, password=raw_password)
|
# Create a UserProfile instance associated with the registered user
|
||||||
|
|
||||||
# Create a UserProfile instance for the user
|
|
||||||
profile = UserProfile(user=user)
|
profile = UserProfile(user=user)
|
||||||
profile.save()
|
profile.save()
|
||||||
|
|
||||||
msg = 'User created - please <a href="/login">login</a>.'
|
msg = 'User created - please <a href="/login">login</a>.' # Set success message with a login link
|
||||||
success = True
|
success = True # Set success flag to True
|
||||||
|
|
||||||
# return redirect("/login/")
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
msg = 'Form is not valid'
|
msg = 'Form is not valid' # Set error message for invalid form data
|
||||||
else:
|
else:
|
||||||
form = SignUpForm()
|
form = SignUpForm() # Create an empty signup form instance for GET requests
|
||||||
|
|
||||||
return render(request, "accounts/register.html", {"form": form, "msg": msg, "success": success})
|
return render(request, "accounts/register.html", {"form": form, "msg": msg, "success": success})
|
||||||
|
|
Loading…
Reference in a new issue