Compare commits
4 commits
bc440dfa2a
...
d39ffc822c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d39ffc822c | ||
|
|
27075d101f | ||
|
|
ea9d38fece | ||
|
|
00f3b61d48 |
7
stadlbauer/forms.py
Normal file
7
stadlbauer/forms.py
Normal 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')
|
||||||
|
|
@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.1/ref/settings/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import os
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
|
@ -54,7 +55,7 @@ ROOT_URLCONF = 'stadlbauer.urls'
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
'DIRS': [],
|
'DIRS': [os.path.join(BASE_DIR, 'templates')],
|
||||||
'APP_DIRS': True,
|
'APP_DIRS': True,
|
||||||
'OPTIONS': {
|
'OPTIONS': {
|
||||||
'context_processors': [
|
'context_processors': [
|
||||||
|
|
@ -99,6 +100,8 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
LOGIN_REDIRECT_URL = '/'
|
||||||
|
LOGOUT_REDIRECT_URL = '/'
|
||||||
|
|
||||||
# Internationalization
|
# Internationalization
|
||||||
# https://docs.djangoproject.com/en/4.1/topics/i18n/
|
# https://docs.djangoproject.com/en/4.1/topics/i18n/
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,13 @@ Including another URLconf
|
||||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path
|
from django.urls import path, include
|
||||||
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
path('', views.index, name='index'),
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
|
path('accounts/new-user/', views.new_user, name='new-user'),
|
||||||
|
path('accounts/profile/', views.profile, name='profile'),
|
||||||
|
path('accounts/', include('django.contrib.auth.urls')),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
24
stadlbauer/views.py
Normal file
24
stadlbauer/views.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
from .forms import SignupForm
|
||||||
|
from django.shortcuts import render
|
||||||
|
from django.urls import reverse
|
||||||
|
from django.http.response import HttpResponseRedirect
|
||||||
|
from django.contrib.auth.decorators import login_required
|
||||||
|
|
||||||
|
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})
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def profile(request):
|
||||||
|
return render(request, 'registration/profile.html')
|
||||||
|
|
||||||
|
def index(request):
|
||||||
|
return render(request, 'index.html')
|
||||||
17
templates/base.html
Normal file
17
templates/base.html
Normal 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>
|
||||||
26
templates/base_main.html
Normal file
26
templates/base_main.html
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block head %}
|
||||||
|
{% load static %}
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li><a href="{% url 'index' %}">Home</a></li>
|
||||||
|
{% if user.is_authenticated %}
|
||||||
|
<li><a href="{% url 'profile' %}">Profile {{ user.username }}</a></li>
|
||||||
|
<li><a href="{% url 'logout' %}">Logout</a></li>
|
||||||
|
{% else %}
|
||||||
|
<li><a href="{% url 'login' %}">Login</a></li>
|
||||||
|
<li><a href="{% url 'new-user' %}">Create account</a></li>
|
||||||
|
{% endif %}
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
<main>
|
||||||
|
{% block main %}
|
||||||
|
{% endblock %}
|
||||||
|
</main>
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
5
templates/index.html
Normal file
5
templates/index.html
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
{% extends "base_main.html" %}
|
||||||
|
{% block title %}Barn{% endblock %}
|
||||||
|
{% block main %}
|
||||||
|
<h1>Barn</h1>
|
||||||
|
{% endblock %}
|
||||||
38
templates/registration/login.html
Normal file
38
templates/registration/login.html
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
{% extends "base_main.html" %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
|
||||||
|
{% 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 log in with an account with the necessary permissions.</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 %}
|
||||||
43
templates/registration/new-user.html
Normal file
43
templates/registration/new-user.html
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
{% extends "base_main.html" %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
{% 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 %}
|
||||||
19
templates/registration/profile.html
Normal file
19
templates/registration/profile.html
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
{% extends "base_main.html" %}
|
||||||
|
{% block title %}Profile {{ user.username }}{% endblock %}
|
||||||
|
{% block main %}
|
||||||
|
<h1>Profile {{ user.username }}</h1>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td>Username</td>
|
||||||
|
<td>{{ user.username }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>First name</td>
|
||||||
|
<td>{{ user.first_name }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Last name</td>
|
||||||
|
<td>{{ user.last_name }}</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
{% endblock %}
|
||||||
Loading…
Reference in a new issue