Add working authorization and registration

This commit is contained in:
vlados31 2022-09-17 00:05:08 +03:00
parent 7582330b1b
commit 59ec0647dc
7 changed files with 43 additions and 19 deletions

View File

@ -6,5 +6,5 @@ from .models import SiteUser
class SiteUserForm(UserCreationForm): class SiteUserForm(UserCreationForm):
class Meta(UserCreationForm.Meta): class Meta(UserCreationForm.Meta):
model = SiteUser model = SiteUser
fields = ('name', 'surname', 'email', 'phone') fields = ('name', 'surname', 'phone', 'email')
error_css_class = 'error' error_css_class = 'error'

View File

@ -19,6 +19,7 @@ from . import views
urlpatterns = [ urlpatterns = [
path('', views.index, name='account'), path('', views.index, name='account'),
path('register', views.register, name='register'),
# path('account', views.account, name='account'), # path('account', views.account, name='account'),
# path('account_<str:action>', views.account_action, name='account_action'), # path('account_<str:action>', views.account_action, name='account_action'),
# #

View File

@ -1,21 +1,24 @@
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse, HttpResponseRedirect
from .forms import SiteUserForm from .forms import SiteUserForm
from django.contrib.auth import login, authenticate
def index(request): def index(request):
return render(request, 'account.html')
def register(request):
if request.method == 'POST': if request.method == 'POST':
form = SiteUserForm(request.POST) form = SiteUserForm(request.POST)
if form.is_valid(): if form.is_valid():
form.save() form.save()
return HttpResponse("User was created successfully.") # # сразу аутентификация
user = authenticate(username=form.username, password=form.password1)
login(request, user)
return HttpResponseRedirect('/account')
else: else:
return HttpResponse("There was an error.") return render(request, 'registration/register.html', {'form': form})
else: else:
form = SiteUserForm() form = SiteUserForm()
return render(request, 'registration/register.html', {'form': form})
return render(request, 'account.html', {'form': form})
# def index(request):
# return render(request, 'account.html')

View File

@ -142,3 +142,4 @@ STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_REDIRECT_URL = '/account/'

View File

@ -3,13 +3,15 @@
{% block content %} {% block content %}
<h1> Ваш аккаунт </h1> <h1> Ваш аккаунт </h1>
<h3>Регистрация</h3> {% if user.is_authenticated %}
<form action="" method="POST">{% csrf_token %} Ваше имя: {{ user.name }}<br>
<table> Ваша фамилия: {{ user.surname }}<br>
<tbody> Ваша почта: {{ user.email }}<br>
{{ form.as_table }} Ваш телефон: {{ user.phone }}<br>
</tbody> {% else %}
</table> Вы не вошли
<button type="submit">Регистрация</button> <a href="{% url 'login' %}">Вход</a>
</form> <a href="{% url 'register' %}">Регистрация</a>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -27,6 +27,15 @@
{% endif %} {% endif %}
</div> </div>
</header> </header>
{% if user.is_authenticated %}
Добро пожаловать, {{ user.name }} {{ user.surname }}<br>
<a href="{% url 'logout'%}?next={{request.path}}">Выход</a>
{% else %}
Вы не вошли
<a href="{% url 'login'%}?next={{request.path}}">Вход</a>
{% endif %}
<nav> <nav>
<a href="{% url 'index' %}">На главную</a> <a href="{% url 'index' %}">На главную</a>
<a href="{% url 'account' %}">Аккаунт</a> <a href="{% url 'account' %}">Аккаунт</a>

View File

@ -1,6 +1,14 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block title %} Аккаунт | вход {% endblock %} {% block title %} Аккаунт | вход {% endblock %}
{% block styles %}
<style>
.error_class {
border: red solid 2px;
}
</style>
{% endblock %}
{% block content %} {% block content %}
<h1> Ваш аккаунт </h1> <h1> Ваш аккаунт </h1>
<h3>Вход</h3> <h3>Вход</h3>