diff --git a/django-user-management/README.md b/django-user-management/README.md new file mode 100644 index 0000000000..d70eeeb218 --- /dev/null +++ b/django-user-management/README.md @@ -0,0 +1,46 @@ +# Get Started With Django User Management + +Follow the [step-by-step instructions](https://realpython.com/django-user-management/) on Real Python. + +## Setup + +You can run the provided example project on your local machine by following the steps outlined below. + +Create a new virtual environment: + +```bash +$ python3 -m venv venv/ +``` + +Activate the virtual environment: + +```bash +$ source venv/bin/activate +``` + +Install the dependencies for this project if you haven't installed them yet: + +```bash +(venv) $ python -m pip install -r requirements.txt +``` + +Navigate into the project's directory: + +```bash +(venv) $ cd user_auth_intro/ +``` + +Make and apply the migrations for the project to build your local database: + +```bash +(venv) $ python manage.py makemigrations +(venv) $ python manage.py migrate +``` + +Run the Django development server: + +```bash +(venv) $ python manage.py runserver +``` + +Navigate to `http://localhost:8000/dashboard` to see the project in action. diff --git a/django-user-management/requirements.txt b/django-user-management/requirements.txt new file mode 100644 index 0000000000..e2ae4cc169 --- /dev/null +++ b/django-user-management/requirements.txt @@ -0,0 +1,3 @@ +asgiref==3.8.1 +Django==5.1.3 +sqlparse==0.5.2 diff --git a/django-user-management/user_auth_intro/manage.py b/django-user-management/user_auth_intro/manage.py new file mode 100755 index 0000000000..c67a51c556 --- /dev/null +++ b/django-user-management/user_auth_intro/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "user_auth_intro.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == "__main__": + main() diff --git a/django-user-management/user_auth_intro/user_auth_intro/__init__.py b/django-user-management/user_auth_intro/user_auth_intro/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django-user-management/user_auth_intro/user_auth_intro/asgi.py b/django-user-management/user_auth_intro/user_auth_intro/asgi.py new file mode 100644 index 0000000000..a86a136712 --- /dev/null +++ b/django-user-management/user_auth_intro/user_auth_intro/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for user_auth_intro project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "user_auth_intro.settings") + +application = get_asgi_application() diff --git a/django-user-management/user_auth_intro/user_auth_intro/settings.py b/django-user-management/user_auth_intro/user_auth_intro/settings.py new file mode 100644 index 0000000000..41db9704f8 --- /dev/null +++ b/django-user-management/user_auth_intro/user_auth_intro/settings.py @@ -0,0 +1,128 @@ +""" +Django settings for user_auth_intro project. + +Generated by 'django-admin startproject' using Django 5.1.3. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.1/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = ( + "django-insecure-%&$tji0geb7vpx8@9-&qbf3_%3$s=5*5amo4tr)yg!6&6@bbhl" +) + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + +EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" + + +# Application definition + +INSTALLED_APPS = [ + "users.apps.UsersConfig", + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "user_auth_intro.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "user_auth_intro.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/5.1/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/5.1/topics/i18n/ + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/5.1/howto/static-files/ + +STATIC_URL = "static/" + +# Default primary key field type +# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/django-user-management/user_auth_intro/user_auth_intro/urls.py b/django-user-management/user_auth_intro/user_auth_intro/urls.py new file mode 100644 index 0000000000..b9a774f5ea --- /dev/null +++ b/django-user-management/user_auth_intro/user_auth_intro/urls.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.urls import include, path + +urlpatterns = [ + path("", include("users.urls")), + path("admin/", admin.site.urls), +] diff --git a/django-user-management/user_auth_intro/user_auth_intro/wsgi.py b/django-user-management/user_auth_intro/user_auth_intro/wsgi.py new file mode 100644 index 0000000000..8413683629 --- /dev/null +++ b/django-user-management/user_auth_intro/user_auth_intro/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for user_auth_intro project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "user_auth_intro.settings") + +application = get_wsgi_application() diff --git a/django-user-management/user_auth_intro/users/__init__.py b/django-user-management/user_auth_intro/users/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django-user-management/user_auth_intro/users/admin.py b/django-user-management/user_auth_intro/users/admin.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django-user-management/user_auth_intro/users/apps.py b/django-user-management/user_auth_intro/users/apps.py new file mode 100644 index 0000000000..88f7b1798e --- /dev/null +++ b/django-user-management/user_auth_intro/users/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UsersConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "users" diff --git a/django-user-management/user_auth_intro/users/forms.py b/django-user-management/user_auth_intro/users/forms.py new file mode 100644 index 0000000000..37ef39ddbd --- /dev/null +++ b/django-user-management/user_auth_intro/users/forms.py @@ -0,0 +1,6 @@ +from django.contrib.auth.forms import UserCreationForm + + +class CustomUserCreationForm(UserCreationForm): + class Meta(UserCreationForm.Meta): + fields = UserCreationForm.Meta.fields + ("email",) diff --git a/django-user-management/user_auth_intro/users/migrations/__init__.py b/django-user-management/user_auth_intro/users/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django-user-management/user_auth_intro/users/models.py b/django-user-management/user_auth_intro/users/models.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django-user-management/user_auth_intro/users/templates/base.html b/django-user-management/user_auth_intro/users/templates/base.html new file mode 100644 index 0000000000..3b233d2011 --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/base.html @@ -0,0 +1,12 @@ + + + + + User Management Intro + + +

Welcome!

+ {% block content %} + {% endblock content %} + + diff --git a/django-user-management/user_auth_intro/users/templates/registration/_logout.html b/django-user-management/user_auth_intro/users/templates/registration/_logout.html new file mode 100644 index 0000000000..938250f08d --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/registration/_logout.html @@ -0,0 +1,6 @@ +
+ {% csrf_token %} + {{ form.as_p }} + + +
diff --git a/django-user-management/user_auth_intro/users/templates/registration/login.html b/django-user-management/user_auth_intro/users/templates/registration/login.html new file mode 100644 index 0000000000..031a80676d --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/registration/login.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} +{% block content %} +

Login

+
+ {% csrf_token %} + {{ form.as_p }} + + +
+

+ Forgot your password? + Back to dashboard +

+{% endblock content %} diff --git a/django-user-management/user_auth_intro/users/templates/registration/password_change_done.html b/django-user-management/user_auth_intro/users/templates/registration/password_change_done.html new file mode 100644 index 0000000000..fe4de16bff --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/registration/password_change_done.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} +{% block content %} +

Password changed

+ Back to dashboard +{% endblock content %} diff --git a/django-user-management/user_auth_intro/users/templates/registration/password_change_form.html b/django-user-management/user_auth_intro/users/templates/registration/password_change_form.html new file mode 100644 index 0000000000..cb7bfa1701 --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/registration/password_change_form.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} +{% block content %} +

Change password

+
+ {% csrf_token %} + {{ form.as_p }} + +
+ Back to dashboard +{% endblock content %} diff --git a/django-user-management/user_auth_intro/users/templates/registration/password_reset_complete.html b/django-user-management/user_auth_intro/users/templates/registration/password_reset_complete.html new file mode 100644 index 0000000000..85c89b5f22 --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/registration/password_reset_complete.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} +{% block content %} +

Password reset completed

+ Login +{% endblock content %} diff --git a/django-user-management/user_auth_intro/users/templates/registration/password_reset_confirm.html b/django-user-management/user_auth_intro/users/templates/registration/password_reset_confirm.html new file mode 100644 index 0000000000..1f98383069 --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/registration/password_reset_confirm.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} +{% block content %} +

Confirm password reset

+
+ {% csrf_token %} + {{ form.as_p }} + +
+ Back to dashboard +{% endblock content %} diff --git a/django-user-management/user_auth_intro/users/templates/registration/password_reset_done.html b/django-user-management/user_auth_intro/users/templates/registration/password_reset_done.html new file mode 100644 index 0000000000..d6aa80f9db --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/registration/password_reset_done.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} +{% block content %} +

Password reset link sent

+ Back to dashboard +{% endblock content %} diff --git a/django-user-management/user_auth_intro/users/templates/registration/password_reset_form.html b/django-user-management/user_auth_intro/users/templates/registration/password_reset_form.html new file mode 100644 index 0000000000..fb3ce5c3e4 --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/registration/password_reset_form.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} +{% block content %} +

Send password reset link

+
+ {% csrf_token %} + {{ form.as_p }} + +
+

+ Back to dashboard +

+{% endblock content %} diff --git a/django-user-management/user_auth_intro/users/templates/registration/sign_up.html b/django-user-management/user_auth_intro/users/templates/registration/sign_up.html new file mode 100644 index 0000000000..66894c1493 --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/registration/sign_up.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} +{% block content %} +

Sign Up

+
+ {% csrf_token %} + {{ form.as_p }} + +
+

+ Back to dashboard +

+{% endblock content %} diff --git a/django-user-management/user_auth_intro/users/templates/users/dashboard.html b/django-user-management/user_auth_intro/users/templates/users/dashboard.html new file mode 100644 index 0000000000..c54024366b --- /dev/null +++ b/django-user-management/user_auth_intro/users/templates/users/dashboard.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} +{% block content %} + Hello, {{ user.username|default:"Guest" }}! +
+ {% if user.is_authenticated %} + {% include "registration/_logout.html" %} + Change password + {% else %} + Login + Sign up + {% endif %} +{% endblock content %} diff --git a/django-user-management/user_auth_intro/users/tests.py b/django-user-management/user_auth_intro/users/tests.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django-user-management/user_auth_intro/users/urls.py b/django-user-management/user_auth_intro/users/urls.py new file mode 100644 index 0000000000..242774ac89 --- /dev/null +++ b/django-user-management/user_auth_intro/users/urls.py @@ -0,0 +1,9 @@ +from django.urls import include, path + +from . import views + +urlpatterns = [ + path("accounts/", include("django.contrib.auth.urls")), + path("dashboard/", views.dashboard, name="dashboard"), + path("sign_up/", views.sign_up, name="sign_up"), +] diff --git a/django-user-management/user_auth_intro/users/views.py b/django-user-management/user_auth_intro/users/views.py new file mode 100644 index 0000000000..fb0c0069c8 --- /dev/null +++ b/django-user-management/user_auth_intro/users/views.py @@ -0,0 +1,23 @@ +from django.contrib.auth import login + +# Remove: from django.contrib.auth.forms import UserCreationForm +from django.shortcuts import redirect, render +from django.urls import reverse + +from .forms import CustomUserCreationForm + + +def dashboard(request): + return render(request, "users/dashboard.html") + + +def sign_up(request): + if request.method == "POST": + form = CustomUserCreationForm(request.POST) + if form.is_valid(): + user = form.save() + login(request, user) + return redirect(reverse("dashboard")) + else: + form = CustomUserCreationForm() + return render(request, "registration/sign_up.html", {"form": form})