Skip to content

jasminedevv/django-accounts-example-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Accounts Example

Read the accompanying article

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.

Requirements

  1. A new (or new-ish) project
  2. Empty database (no existing users)
  3. Python 3

What it does

This app only adds 3 routes: login, logout, and signup. Any other urls should return a 404 (unless handled by other apps).

That's it! That's all it does.

Steps

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.

in settings.py

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'

in urls.py (main app)

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')),
]

in terminal

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.

in other apps

To use the custom user model in other apps:

from django.contrib.auth import get_user_model
User = get_user_model()

About

An example app for handling users in Django.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published