implement login form

This commit is contained in:
James Ravenscroft 2024-12-07 15:18:02 +00:00
parent 3d12cd52d6
commit aeb9e58ccf
4 changed files with 91 additions and 33 deletions

View File

@ -1,6 +1,19 @@
from django import forms
from .models import User
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
class LoginForm(AuthenticationForm):
class Meta:
model = User
fields = ['email', 'password']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['email'].widget.attrs.update(
{'class': 'shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline'})
self.fields['password'].widget.attrs.update(
{'class': 'shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline'})
class RegisterForm(UserCreationForm):

View File

@ -80,7 +80,9 @@
<div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
<p class="text-center text-gray-600 text-sm">
Already have an account?
<a href="#" class="text-blue-500 hover:text-blue-600">Sign in</a>
<a href="{% url 'login' %}" class="text-blue-500 hover:text-blue-600"
>Sign in</a
>
</p>
</div>
</div>

View File

@ -0,0 +1,74 @@
{% extends "main.html" %} {% block content %}
<main class="container mx-auto px-6 py-8">
<div class="max-w-md mx-auto bg-white rounded-lg overflow-hidden shadow-lg">
<div class="px-6 py-8">
<h2 class="text-2xl font-bold text-center text-gray-800 mb-8">
Log in to AnnoMemo
</h2>
{% if form.non_field_errors %}
<div
class="mb-4 p-2 bg-red-100 border border-red-400 text-red-700 rounded"
>
{% for error in form.non_field_errors %} {{ error }} {% endfor %}
</div>
{% endif %}
<form method="POST" action="{% url 'login' %}">
{% csrf_token %}
<div class="mb-4">
<label
for="{{ form.username.id_for_label }}"
class="block text-gray-700 text-sm font-bold mb-2"
>Email Address</label
>
<input
type="text"
name="{{ form.username.html_name }}"
id="{{ form.username.id_for_label }}"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
/>
{% if form.username.errors %}
<p class="text-red-500 text-xs italic">
{{ form.username.errors.0 }}
</p>
{% endif %}
</div>
<div class="mb-6">
<label
for="{{ form.password.id_for_label }}"
class="block text-gray-700 text-sm font-bold mb-2"
>Password</label
>
<input
type="password"
name="{{ form.password.html_name }}"
id="{{ form.password.id_for_label }}"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
/>
{% if form.password.errors %}
<p class="text-red-500 text-xs italic">
{{ form.password.errors.0 }}
</p>
{% endif %}
</div>
<div class="flex items-center justify-between">
<button
type="submit"
class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline w-full"
>
Log In
</button>
</div>
</form>
</div>
<div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
<p class="text-center text-gray-600 text-sm">
Don't have an account?
<a href="{% url 'register' %}" class="text-blue-500 hover:text-blue-600"
>Sign up</a
>
</p>
</div>
</div>
</main>
{% endblock %}

View File

@ -33,34 +33,3 @@ def register(request: HttpRequest):
form = RegisterForm()
return render(request, 'register.html', {'form': form})
# email = request.POST.get('email')
# password = request.POST.get('password')
# confirm_password = request.POST.get('confirm_password')
# errors = False
# if not email:
# messages.error(request, 'Email is required')
# errors = True
# if not password or len(password) < 8:
# messages.error(request, 'Password must be at least 8 characters long')
# errors = True
# if password != confirm_password:
# messages.error(request, 'Passwords do not match')
# errors = True
# if not errors:
# if User.objects.filter(email=email).exists():
# messages.error(request, 'Email already exists')
# else:
# user = User.objects.create_user(
# username=username, email=email, password=password) # type: ignore
# user.save()
# messages.success(request, 'Account created successfully')
# return redirect('login')
return render(request, 'register.html')