Implement new-user and login

This commit is contained in:
Benjamin 2022-12-20 21:29:43 +01:00
parent adca4da77b
commit 1cc8f602a8
7 changed files with 127 additions and 2 deletions

7
stadlbauer/forms.py Normal file
View file

@ -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')

View file

@ -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': [

View file

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

16
stadlbauer/views.py Normal file
View file

@ -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})

17
templates/base.html Normal file
View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% load static %}
{% block head %}{% endblock %}
<title>{% block title %}Barn{% endblock %}</title>
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>

View file

@ -0,0 +1,38 @@
{% extends "base.html" %}
{% block body %}
{% if form.errors %}
<section>
<p>Username and password do not match. Please try again.</p>
</section>
{% endif %}
{% if next %}
<section>
{% if user.is_authenticated %}
<p>You are not authorized to access this site. Please inform Benjamin to get the corresponding authorization.</p>
{% else %}
<p>Please log in to view this site.</p>
{% endif %}
</section>
{% endif %}
<form action="{% url 'login' %}" method="post">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="Login">
<input type="hidden" name="next" value="{{ next }}">
</form>
{% endblock %}

View file

@ -0,0 +1,43 @@
{% extends "base.html" %}
{% block body %}
{% if form.errors %}
<section>
<h2>Error:</h2>
<ul>
{% for field in form %}
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
</section>
{% endif %}
<form method="post" action="{% url 'new-user' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.first_name.label_tag }}</td>
<td>{{ form.first_name }}</td>
</tr>
<tr>
<td>{{ form.last_name.label_tag }}</td>
<td>{{ form.last_name }}</td>
</tr>
<tr>
<td>{{ form.password1.label_tag }}</td>
<td>{{ form.password1 }}</td>
</tr>
<tr>
<td>{{ form.password2.label_tag }}</td>
<td>{{ form.password2 }}</td>
</tr>
</table>
<input type="submit" value="Create account">
</form>
{% endblock %}