This is an example app module that can be used in a Django project to handle authentication and account creation for users. It contains a custom user model and a profile model which can be customized depending on your project's needs.
I created this with students in mind but more advanced developers may find handy code snippets. It could also work as a hackathon starter.
- A new (or new-ish) project
- Empty database (no existing users)
- Python 3
This app only adds 3 routes: login, logout, and signup. Any other urls should return a 404 (unless handled by other apps).
- Go to http://localhost:8000/accounts/signup/ to create an account,
- Go to http://localhost:8000/accounts/logout to test the logout functionality,
- Try signing in again at http://localhost:8000/accounts/login/.
That's it! That's all it does.
Start by copying this app folder into the project root. To create a new project from scratch try:
$ django-admin startproject
(will either need to have django installed globally or be in an active virtualenv with django installed)
Next, rename the folder from 'django-accounts-example-app' to just 'accounts'.
Side note: I would recommend deleting the .git folder above messing around with submodules. This project is meant to be a baseline to build your own thing off of, not a package.
Add 'accounts' to INSTALLED_APPS:
INSTALLED_APPS = [
'django.contrib.admin',
...
'accounts',
]
So Django uses the custom user model instead of the default one:
AUTH_USER_MODEL = 'accounts.User'
Add this to your main url dispatcher (the urls.py in your project's main app):
from django.urls import include, path
urlpatterns = [
path('accounts/', include('accounts.urls')),
]
Run:
$ python manage.py makemigrations accounts && python manage.py migrate
This command will not work (and probably break things) if you already have users in your database.
To use the custom user model in other apps:
from django.contrib.auth import get_user_model
User = get_user_model()