From 00f3b61d48e068ce52eca302708a3de11c72669f Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 20 Dec 2022 21:29:43 +0100 Subject: [PATCH] Implement new-user and login --- stadlbauer/forms.py | 7 +++++ stadlbauer/settings.py | 3 +- stadlbauer/urls.py | 5 +++- stadlbauer/views.py | 16 +++++++++++ templates/base.html | 17 +++++++++++ templates/registration/login.html | 38 ++++++++++++++++++++++++ templates/registration/new-user.html | 43 ++++++++++++++++++++++++++++ 7 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 stadlbauer/forms.py create mode 100644 stadlbauer/views.py create mode 100644 templates/base.html create mode 100644 templates/registration/login.html create mode 100644 templates/registration/new-user.html diff --git a/stadlbauer/forms.py b/stadlbauer/forms.py new file mode 100644 index 0000000..5bf0711 --- /dev/null +++ b/stadlbauer/forms.py @@ -0,0 +1,7 @@ +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User + +class SignupForm(UserCreationForm): + class Meta: + model = User + fields = ('username', 'password1', 'password2', 'first_name', 'last_name') diff --git a/stadlbauer/settings.py b/stadlbauer/settings.py index 44f6aef..da8f6ab 100644 --- a/stadlbauer/settings.py +++ b/stadlbauer/settings.py @@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.1/ref/settings/ """ from pathlib import Path +import os # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -54,7 +55,7 @@ ROOT_URLCONF = 'stadlbauer.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ diff --git a/stadlbauer/urls.py b/stadlbauer/urls.py index 440e147..4074b78 100644 --- a/stadlbauer/urls.py +++ b/stadlbauer/urls.py @@ -14,8 +14,11 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include +from . import views urlpatterns = [ path('admin/', admin.site.urls), + path('accounts/new-user/', views.new_user, name='new-user'), + path('accounts/', include('django.contrib.auth.urls')), ] diff --git a/stadlbauer/views.py b/stadlbauer/views.py new file mode 100644 index 0000000..8a9fa3e --- /dev/null +++ b/stadlbauer/views.py @@ -0,0 +1,16 @@ +from .forms import SignupForm +from django.shortcuts import render +from django.urls import reverse +from django.http.response import HttpResponseRedirect + +def new_user(request): + if request.method == 'POST': + form = SignupForm(request.POST) + + if form.is_valid(): + form.save(commit=True) + return HttpResponseRedirect(reverse('login')) + else: + form = SignupForm() + + return render(request, 'registration/new-user.html', {'form': form}) diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..7269a4f --- /dev/null +++ b/templates/base.html @@ -0,0 +1,17 @@ + + + + + + + + {% load static %} + {% block head %}{% endblock %} + {% block title %}Barn{% endblock %} + + + + {% block body %}{% endblock %} + + + diff --git a/templates/registration/login.html b/templates/registration/login.html new file mode 100644 index 0000000..86f5b38 --- /dev/null +++ b/templates/registration/login.html @@ -0,0 +1,38 @@ +{% extends "base.html" %} + +{% block body %} + +{% if form.errors %} +
+

Username and password do not match. Please try again.

+
+{% endif %} + +{% if next %} +
+ {% if user.is_authenticated %} +

You are not authorized to access this site. Please log in with an account with the necessary permissions.

+ {% else %} +

Please log in to view this site.

+ {% endif %} +
+{% endif %} + +
+ {% csrf_token %} + + + + + + + + + +
{{ form.username.label_tag }}{{ form.username }}
{{ form.password.label_tag }}{{ form.password }}
+ + + +
+ +{% endblock %} diff --git a/templates/registration/new-user.html b/templates/registration/new-user.html new file mode 100644 index 0000000..0f8d670 --- /dev/null +++ b/templates/registration/new-user.html @@ -0,0 +1,43 @@ +{% extends "base.html" %} + +{% block body %} + {% if form.errors %} +
+

Error:

+ +
+ {% endif %} + +
+ {% csrf_token %} + + + + + + + + + + + + + + + + + + + + + +
{{ form.username.label_tag }}{{ form.username }}
{{ form.first_name.label_tag }}{{ form.first_name }}
{{ form.last_name.label_tag }}{{ form.last_name }}
{{ form.password1.label_tag }}{{ form.password1 }}
{{ form.password2.label_tag }}{{ form.password2 }}
+ +
+{% endblock %}