From 59ec0647dc2c8b0153a4626ace8cc68c5530962c Mon Sep 17 00:00:00 2001 From: vlados31 Date: Sat, 17 Sep 2022 00:05:08 +0300 Subject: [PATCH] Add working authorization and registration --- account/forms.py | 2 +- account/urls.py | 1 + account/views.py | 21 ++++++++++++--------- arka/settings.py | 1 + templates/account.html | 20 +++++++++++--------- templates/base.html | 9 +++++++++ templates/registration/login.html | 8 ++++++++ 7 files changed, 43 insertions(+), 19 deletions(-) diff --git a/account/forms.py b/account/forms.py index 5e72194..5d4aa5b 100644 --- a/account/forms.py +++ b/account/forms.py @@ -6,5 +6,5 @@ from .models import SiteUser class SiteUserForm(UserCreationForm): class Meta(UserCreationForm.Meta): model = SiteUser - fields = ('name', 'surname', 'email', 'phone') + fields = ('name', 'surname', 'phone', 'email') error_css_class = 'error' diff --git a/account/urls.py b/account/urls.py index f7d8080..e30a8c1 100644 --- a/account/urls.py +++ b/account/urls.py @@ -19,6 +19,7 @@ from . import views urlpatterns = [ path('', views.index, name='account'), + path('register', views.register, name='register'), # path('account', views.account, name='account'), # path('account_', views.account_action, name='account_action'), # diff --git a/account/views.py b/account/views.py index d29f4b5..560fe27 100644 --- a/account/views.py +++ b/account/views.py @@ -1,21 +1,24 @@ from django.shortcuts import render -from django.http import HttpResponse +from django.http import HttpResponse, HttpResponseRedirect from .forms import SiteUserForm +from django.contrib.auth import login, authenticate def index(request): + return render(request, 'account.html') + + +def register(request): if request.method == 'POST': form = SiteUserForm(request.POST) if form.is_valid(): form.save() - return HttpResponse("User was created successfully.") + # # сразу аутентификация + user = authenticate(username=form.username, password=form.password1) + login(request, user) + return HttpResponseRedirect('/account') else: - return HttpResponse("There was an error.") + return render(request, 'registration/register.html', {'form': form}) else: form = SiteUserForm() - - return render(request, 'account.html', {'form': form}) - - -# def index(request): -# return render(request, 'account.html') + return render(request, 'registration/register.html', {'form': form}) diff --git a/arka/settings.py b/arka/settings.py index f7b1ecb..3d5834b 100644 --- a/arka/settings.py +++ b/arka/settings.py @@ -142,3 +142,4 @@ STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")] DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' +LOGIN_REDIRECT_URL = '/account/' diff --git a/templates/account.html b/templates/account.html index aed0c6e..81c4da5 100644 --- a/templates/account.html +++ b/templates/account.html @@ -3,13 +3,15 @@ {% block content %}

Ваш аккаунт

-

Регистрация

-
{% csrf_token %} - - - {{ form.as_table }} - -
- -
+ {% if user.is_authenticated %} + Ваше имя: {{ user.name }}
+ Ваша фамилия: {{ user.surname }}
+ Ваша почта: {{ user.email }}
+ Ваш телефон: {{ user.phone }}
+ {% else %} + Вы не вошли + Вход + Регистрация + {% endif %} + {% endblock %} diff --git a/templates/base.html b/templates/base.html index 1e5471a..0356383 100644 --- a/templates/base.html +++ b/templates/base.html @@ -27,6 +27,15 @@ {% endif %} + + {% if user.is_authenticated %} + Добро пожаловать, {{ user.name }} {{ user.surname }}
+ Выход + {% else %} + Вы не вошли + Вход + {% endif %} +