Django ships with a built-in User model for authentication and if you’d like a basic tutorial on how to implement log in, log out, sign up
Commands
$ cd ~/Desktop
$ mkdir accounts && cd accounts
$ pipenv install django~=3.1.0
$ pipenv shell
(accounts) $ django-admin.py startproject config .
(accounts) $ python manage.py startapp accounts
(accounts) $ python manage.py runserver
There are two modern ways to create a custom user model in Django: AbstractUser and AbstractBaseUser.
In both cases we can subclass them to extend existing functionality however AbstractBaseUser requires much, much more work.
Installed Apps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts', # new
]
...
AUTH_USER_MODEL = 'accounts.CustomUser'
update accounts/models.py with a new User model which we’ll call CustomUser.
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
pass
# add additional fields in here
def __str__(self):
return self.username
installation:
(accounts) $ python manage.py createsuperuser
Templates/Views/URLs
TEMPLATES = [
{
...
'DIRS': [str(BASE_DIR.joinpath('templates'))], # new
...
},
]
urls.py file
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls')),
path('accounts/', include('django.contrib.auth.urls')),
]
$ git clone https://github.com/wsvincent/djangox.git
$ cd djangox
$ python3 -m venv djangox
$ source djangox/bin/activate
(djangox) $ pip install -r requirements.txt
(djangox) $ python manage.py migrate
(djangox) $ python manage.py createsuperuser
(djangox) $ python manage.py runserver
# Load the site at http://127.0.0.1:8000
$ pipenv install
$ pipenv shell
(djangox) $ python manage.py migrate
(djangox) $ python manage.py createsuperuser
(djangox) $ python manage.py runserver
# Load the site at http://127.0.0.1:8000
$ docker build .
$ docker-compose up -d
$ docker-compose exec web python manage.py migrate
$ docker-compose exec web python manage.py createsuperuser
# Load the site at http://127.0.0.1:8000
# Run Migrations
(djangox) $ python manage.py migrate
# Create a Superuser
(djangox) $ python manage.py createsuperuser
# Confirm everything is working:
(djangox) $ python manage.py runserver
# Load the site at http://127.0.0.1:8000