added simple registration form
All checks were successful
Django Backend Validation / build (debian-latest, 3.10) (push) Successful in 20s
Django Backend Validation / build (debian-latest, 3.11) (push) Successful in 21s
Django Backend Validation / build (debian-latest, 3.12) (push) Successful in 17s
Django Backend Validation / build (ubuntu-latest, 3.10) (push) Successful in 12s
Django Backend Validation / build (ubuntu-latest, 3.11) (push) Successful in 12s
Django Backend Validation / build (ubuntu-latest, 3.12) (push) Successful in 13s

Signed-off-by: Jochen Mehlich <coding@jochenmehlich.de>
This commit is contained in:
Jochen Mehlich 2024-08-29 16:27:18 +02:00
parent a9d639db55
commit efc9b8e10b
4 changed files with 64 additions and 2 deletions

View File

@ -0,0 +1,36 @@
{%extends "base.html" %}
{% block content %}
<section class="bg-gray-50 dark:bg-gray-900">
<div class="flex flex-col items-center justify-center px-6 py-8 mx-auto md:h-screen lg:py-0">
<div class="w-full bg-white rounded-lg shadow dark:border md:mt-0 sm:max-w-md xl:p-0 dark:bg-gray-800 dark:border-gray-700">
<div class="p-6 space-y-4 md:space-y-6 sm:p-8">
<h1 class="text-xl font-bold leading-tight tracking-tight text-gray-900 md:text-2xl dark:text-white">
Create your account
</h1>
<form class="space-y-4 md:space-y-6" method="post">
<input type="hidden" name="auth_type" value="form">
{% csrf_token %}
<div>
<label for="username" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your username</label>
<input type="text" name="username" id="username" class="bg-gray-50 border border-gray-300 text-gray-900 rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="fooboar" required="">
</div>
<div>
<label for="email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your email</label>
<input type="email" name="email" id="email" class="bg-gray-50 border border-gray-300 text-gray-900 rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="foo@bar.it" required="">
</div>
<div>
<label for="password" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Password</label>
<input type="password" name="password" id="password" placeholder="••••••••" class="bg-gray-50 border border-gray-300 text-gray-900 rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" required="">
</div>
<button type="submit" class="w-full inline-flex items-center justify-center p-0.5 me-2 overflow-hidden text-sm font-medium text-gray-900 rounded-lg group bg-gradient-to-br from-purple-600 to-blue-500 group-hover:from-purple-600 group-hover:to-blue-500 hover:text-white dark:text-white focus:ring-4 focus:outline-none focus:ring-blue-300 dark:focus:ring-blue-800">
<span class="w-full px-5 py-2.5 transition-all ease-in duration-75 bg-white dark:bg-gray-900 rounded-md group-hover:bg-opacity-0">
Signup
</span>
</button>
</form>
</div>
</div>
</div>
</section>
{% endblock content %}

View File

@ -4,4 +4,10 @@ from django.contrib.auth.models import User
class LoginForm(forms.Form): class LoginForm(forms.Form):
username = forms.CharField(label="username", max_length=255) username = forms.CharField(label="username", max_length=255)
password = forms.CharField(label="password", max_length=255, widget=forms.PasswordInput) password = forms.CharField(label="password", max_length=255, widget=forms.PasswordInput)
auth_type = forms.CharField(label="auth_type", max_length=255) auth_type = forms.CharField(label="auth_type", max_length=255)
class RegisterForm(forms.Form):
username = forms.CharField(label="username", max_length=255)
password = forms.CharField(label="password", max_length=255, widget=forms.PasswordInput)
email = forms.CharField(label="email", max_length=255)

View File

@ -22,6 +22,7 @@ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path("__reload__/", include("django_browser_reload.urls")), path("__reload__/", include("django_browser_reload.urls")),
path("login", login, name="login"), path("login", login, name="login"),
path("register", register, name="register"),
path("logout", logout, name="logout"), path("logout", logout, name="logout"),
path("settings/", include('settings.urls')), path("settings/", include('settings.urls')),
] ]

View File

@ -1,8 +1,9 @@
from django.shortcuts import render from django.shortcuts import render
import django.contrib.auth as dauth import django.contrib.auth as dauth
from django.shortcuts import redirect from django.shortcuts import redirect
from django.contrib.auth.models import User
from zitate.forms import LoginForm from zitate.forms import LoginForm, RegisterForm
def login(request): def login(request):
@ -24,6 +25,24 @@ def login(request):
elif auth_type == "openid": elif auth_type == "openid":
return render(request, "login.html") return render(request, "login.html")
def register(request):
if request.method == "GET":
return render(request, "register.html")
elif request.method == "POST":
form = RegisterForm(request.POST)
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
email = form.cleaned_data["email"]
User.objects.create_user(username, email, password)
user = dauth.authenticate(request, username=username, password=password)
if user is not None:
dauth.login(request, user)
return redirect("/")
else:
return render(request, "register.html", {"userError": True})
def logout(request): def logout(request):
dauth.logout(request) dauth.logout(request)
return redirect("/") return redirect("/")