1st commit

This commit is contained in:
2026-05-25 19:45:43 -03:00
commit 55087c8278
44 changed files with 1522 additions and 0 deletions

0
accounts/__init__.py Normal file
View File

3
accounts/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
accounts/apps.py Normal file
View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class AccountsConfig(AppConfig):
name = 'accounts'

14
accounts/forms.py Normal file
View File

@@ -0,0 +1,14 @@
from django import forms
from django.contrib.auth.models import User
class UserRegistrationForm(forms.Form):
username = forms.CharField(label='Username', max_length=100, min_length=5,
widget=forms.TextInput(attrs={'class': 'form-control'}))
email = forms.EmailField(label='Email', max_length=35, min_length=5,
widget=forms.EmailInput(attrs={'class': 'form-control'}))
password1 = forms.CharField(label='Password', max_length=50, min_length=5,
widget=forms.PasswordInput(attrs={'class': 'form-control'}))
password2 = forms.CharField(label='Confirm Password',
max_length=50, min_length=5,
widget=forms.PasswordInput(attrs={'class': 'form-control'}))

View File

3
accounts/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,41 @@
{% extends 'base.html' %}
{% block content %}
<div class="vh-100 d-flex justify-content-center align-items-center p-5" id="login-content">
<div class="col-md-5 p-5 shadow-sm border rounded-5 border-primary bg-white">
<h2 class="text-center mb-4 text-primary">Login</h2>
{% if messages %}
<div class="messages">
{% for message in messages %}
<div {% if message.tags %} class="{{ message.tags }}" {% endif %}>{{ message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% endfor %}
</div>
{% endif %}
<form action="" method="post">
{% csrf_token %}
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control border border-primary" id="username"
aria-describedby="username" name="username" placeholder="Enter Username" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control border border-primary" name="password"
placeholder="Password" required>
</div>
<div class="d-grid">
<button class="btn btn-primary" type="submit">Login</button>
</div>
</form>
<div class="mt-3">
<p class="mb-0 text-center">Don't have an account? <a href="{% url 'accounts:register' %}"
class="text-primary fw-bold">Sign
Up</a></p>
</div>
</div>
</div>
{% endblock %}

View File

@@ -0,0 +1,27 @@
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="row center">
<div class="col-md-5 mx-auto p-5 shadow-sm border rounded border-primary">
<p>Already have an account? <a href="{% url 'accounts:login' %}">Login Here</a></p>
{% if messages %}
<div class="messages">
{% for message in messages %}
<div {% if message.tags %} class="{{ message.tags }}" {% endif %}>{{ message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% endfor %}
</div>
{% endif %}
<form action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">Sign Up</button>
</form>
</div>
</div>
</div>
{% endblock %}

3
accounts/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

10
accounts/urls.py Normal file
View File

@@ -0,0 +1,10 @@
from django.urls import path
from . import views
app_name = "accounts"
urlpatterns=[
path('login/', views.login_user, name='login'),
path('logout/', views.logout_user, name='logout'),
path('register/', views.create_user, name='register'),
]

68
accounts/views.py Normal file
View File

@@ -0,0 +1,68 @@
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from .forms import UserRegistrationForm
from django.contrib import messages
from django.http import HttpResponse
def login_user(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
redirect_url = request.GET.get('next', 'home')
return redirect(redirect_url)
else:
messages.error(request, "Username Or Password is incorrect!",
extra_tags='alert alert-warning alert-dismissible fade show')
return render(request, 'accounts/login.html')
def logout_user(request):
logout(request)
return redirect('home')
def create_user(request):
if request.method == 'POST':
check1 = False
check2 = False
check3 = False
form = UserRegistrationForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password1 = form.cleaned_data['password1']
password2 = form.cleaned_data['password2']
email = form.cleaned_data['email']
if password1 != password2:
check1 = True
messages.error(request, 'Password did not match!',
extra_tags='alert alert-warning alert-dismissible fade show')
if User.objects.filter(username=username).exists():
check2 = True
messages.error(request, 'Username already exists!',
extra_tags='alert alert-warning alert-dismissible fade show')
if User.objects.filter(email=email).exists():
check3 = True
messages.error(request, 'Email already registered!',
extra_tags='alert alert-warning alert-dismissible fade show')
if check1 or check2 or check3:
messages.error(
request, "Registration Failed!", extra_tags='alert alert-warning alert-dismissible fade show')
return redirect('accounts:register')
else:
user = User.objects.create_user(
username=username, password=password1, email=email)
messages.success(
request, f'Thanks for registering {user.username}.', extra_tags='alert alert-success alert-dismissible fade show')
return redirect('accounts:login')
else:
form = UserRegistrationForm()
return render(request, 'accounts/register.html', {'form': form})