django_real_time_validation
Django and Ajax: Robust authentication and authorization system with real-time form validations for web applications
This website collects cookies to deliver better user experience
Build a multi-user authentication system. A user can either be a Student or a Lecturer. For students, their usernames must start with CPE and must be at least 9 characters long not counting any forward slash that it contains. As for Lecturers, only their emails should be filled in and to be valid, all email addresses must end with @futa.edu.ng. Lecturers' usernames should be autogenerated and must start with the words before @
in the email address provided. Email addresses must be unique and confirmed by sending confirmation mails upon registration and the mails must support HTML. Until confirmation, no user is allowed to log in. Time attacks must be addressed by sending the mails asynchronously. Users can either login with any of Email/Password or Username/Password combination. Password reset functionality should be incorporated as well. At first login, Lecturers should be prompted to complete their profiles.
NOTE
I have created a project named authentication
, and an application, accounts
. Both have been linked and static
as well as templates
configured. You are enjoined to do these before proceeding. Current folder structure is as follows:
└── accounts/
│ ├──── admin.py
│ ├──── apps.py
│ ├──── __init__.py
│ └──── migrations/
│ │ └──── __init__.py
│ ├──── models.py
│ ├──── tests.py
│ └──── views.py
└── authentication/
│ ├──── asgi.py
│ ├──── __init__.py
│ ├──── settings.py
│ ├──── urls.py
│ └──── wsgi.py
├── manage.py
├── Pipfile
├── Pipfile.lock
├── Procfile
├── setup.cfg
└── static/
│ └──── css/
│ │ ├──── materialize.min.css
│ │ └──── style.css
│ └──── img/
│ │ └──── sign-up-illustration.svg
│ └──── js/
│ │ ├──── init.js
│ │ ├──── jquery.min.js
│ │ └──── materialize.min.js
└── templates/
│ └──── accounts/
│ │ └──── index.html
│ ├──── base.html
│ └──── includes/
│ │ ├──── _footer.html
│ │ └──── _header.html
You can get this full starter template structure from github. It should be noted that the structure also contains some configurations for sending emails with gmail. Check this dev.to post for details about that.
Django and Ajax: Robust authentication and authorization system with real-time form validations for web applications
Build a multi-user authentication system. A user can either be a Student or a Lecturer ...
AbstractUser
inheritance. This gives us the inherent robustness of the built-in django user model while allowing a nifty and clean extension. A good resource for working with this approach and others can be found in this Vitor Freitas's article.accounts/models.py
file and transform it to something like this listing:# accounts > models.py
import uuid
from django.contrib.auth.models import AbstractUser
from django.db import models
LEVEL = (
("100L", "100L"),
("200L", "200L"),
("300L", "300L"),
("400L", "400L"),
)
ALIAS = (("Mr.", "Mr."), ("Mrs", "Mrs"), ("Dr.", "Dr."), ("Prof.", "Prof."))
GENDER = (
("Male", "Male"),
("Female", "Female"),
("Prefer not to mention", "Prefer not to mention"),
)
class User(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
is_student = models.BooleanField(default=False)
is_lecturer = models.BooleanField(default=False)
alias = models.CharField(choices=ALIAS, max_length=5, null=True, blank=True)
level = models.CharField(choices=LEVEL, max_length=11, null=True, blank=True)
gender = models.CharField(choices=GENDER, max_length=22)
has_logged_in = models.BooleanField(default=False)
def __str__(self):
return str(self.id)
┌──(sirneij@sirneij)-[~/Documents/Projects/Django/django_real_time_validation]
└─$[sirneij@sirneij django_real_time_validation]$ python manage.py makemigrations
SystemCheckError: System check identified some issues:
ERRORS:
accounts.User.groups: (fields.E304) Reverse accessor for 'accounts.User.groups' clashes with reverse accessor for 'auth.User.groups'.
HINT: Add or change a related_name argument to the definition for 'accounts.User.groups' or 'auth.User.groups'.
accounts.User.user_permissions: (fields.E304) Reverse accessor for 'accounts.User.user_permissions' clashes with reverse accessor for 'auth.User.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'accounts.User.user_permissions' or 'auth.User.user_permissions'.
auth.User.groups: (fields.E304) Reverse accessor for 'auth.User.groups' clashes with reverse accessor for 'accounts.User.groups'.
HINT: Add or change a related_name argument to the definition for 'auth.User.groups' or 'accounts.User.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'auth.User.user_permissions' clashes with reverse accessor for 'accounts.User.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'auth.User.user_permissions' or 'accounts.User.user_permissions'.
settings.py
file:# authentication > settings.py
...
AUTH_USER_MODEL = "accounts.User"
...
┌──(sirneij@sirneij)-[~/Documents/Projects/Django/django_real_time_validation]
└─$[sirneij@sirneij django_real_time_validation]$ python manage.py makemigrations
Migrations for 'accounts':
accounts/migrations/0001_initial.py
- Create model User
migrate
.