From b3988797c52e0c5302a91e4f1ebc9f1815c0f84e Mon Sep 17 00:00:00 2001 From: root Date: Mon, 13 Oct 2025 22:35:39 +0300 Subject: [PATCH] feat: auth --- articles/static/index.css | 29 ++++++++++++++++ articles/templates/archive.html | 20 +++++++++-- articles/templates/login.html | 29 ++++++++++++++++ articles/templates/registration.html | 30 ++++++++++++++++ articles/views.py | 50 +++++++++++++++++++++++++++ blog/urls.py | 3 ++ db.sqlite3 | Bin 188416 -> 188416 bytes 7 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 articles/templates/login.html create mode 100644 articles/templates/registration.html diff --git a/articles/static/index.css b/articles/static/index.css index 170f448..a51b194 100644 --- a/articles/static/index.css +++ b/articles/static/index.css @@ -21,6 +21,21 @@ a { gap: 10px; } +.auth-header { + display: flex; + justify-content: space-between; + align-items: center; +} + +.logo-div { + display: flex; + gap: 10px; +} + +.login-sep { + display: inline; +} + .one-post { background: #273449; border: solid 1px #334155; @@ -74,4 +89,18 @@ form { .error { color: red; +} + +.login-form { + max-width: 400px; + margin: auto; +} + +.registration-header { + text-align: center; +} + +.registration-error { + text-align: center; + margin-top: 10px; } \ No newline at end of file diff --git a/articles/templates/archive.html b/articles/templates/archive.html index c82e5bd..67b2e8b 100644 --- a/articles/templates/archive.html +++ b/articles/templates/archive.html @@ -9,9 +9,23 @@ -
- -

Всё про IT

+
+
+ +

Всё про IT

+
+ +
+ {% block auth %} + {% if not user.is_anonymous %} + Выйти + {% else %} + Вход + + Регистрация + {% endif %} + {% endblock %} +
{% block write %} {% if not user.is_anonymous %} diff --git a/articles/templates/login.html b/articles/templates/login.html new file mode 100644 index 0000000..2b9ff00 --- /dev/null +++ b/articles/templates/login.html @@ -0,0 +1,29 @@ + + + + + + Регистрация + {% load static %} + + + + +
+ +

Всё про IT

+
+
+

Вход

+ +
+ {{ error }} +
+
+

← Назад к списку статей

+ + \ No newline at end of file diff --git a/articles/templates/registration.html b/articles/templates/registration.html new file mode 100644 index 0000000..1ce1329 --- /dev/null +++ b/articles/templates/registration.html @@ -0,0 +1,30 @@ + + + + + + Регистрация + {% load static %} + + + + +
+ +

Всё про IT

+
+
+

Регистрация

+ +
+ {{ error }} +
+
+

← Назад к списку статей

+ + \ No newline at end of file diff --git a/articles/views.py b/articles/views.py index c51e1fd..863d751 100644 --- a/articles/views.py +++ b/articles/views.py @@ -2,6 +2,8 @@ from .models import Article from django.shortcuts import render from django.http import Http404, HttpResponse from django.shortcuts import redirect +from django.contrib.auth.models import User +from django.contrib.auth import authenticate, login, logout def archive(request): return render(request, 'archive.html', {"posts": Article.objects.all()}) @@ -32,3 +34,51 @@ def create_post(request): return render(request, 'new_article.html') else: return HttpResponse('Unauthorized', status=401) + +def registration(request): + if request.method == "POST": + form = { 'username': request.POST["username"], 'email': request.POST["email"], 'password': request.POST["password"] } + username = request.POST.get("username", "").strip() + email = request.POST.get("email", "").strip() + password = request.POST.get("password", "").strip() + if not username or not email or not password: + return render(request, 'registration.html', {'error': 'Все поля обязательны для заполнения.', 'form': form}) + + if len(password) < 6: + return render(request, 'registration.html', {'error': 'Пароль должен содержать не менее 6 символов.', 'form': form}) + + if User.objects.filter(username=username).exists(): + return render(request, 'registration.html', {'error': 'Пользователь с таким именем уже существует.', 'form': form}) + + if User.objects.filter(email=email).exists(): + return render(request, 'registration.html', {'error': 'Этот email уже используется.', 'form': form}) + + if "@" not in email or "." not in email: + return render(request, 'registration.html', {'error': 'Некорректный формат email.', 'form': form}) + + user = User.objects.create_user(username=username, email=email, password=password) + user.save() + return redirect('/login') + return render(request, 'registration.html') + +def login_page(request): + if request.method == "POST": + form = {'username': request.POST.get("username"), 'password': request.POST.get("password")} + username = request.POST.get("username", "").strip() + password = request.POST.get("password", "").strip() + + if not username or not password: + return render(request, 'login.html', {'error': 'Введите имя пользователя и пароль.', 'form': form}) + + user = authenticate(request, username=username, password=password) + + if user is None: + return render(request, 'login.html', {'error': 'Неверное имя пользователя или пароль.', 'form': form}) + + login(request, user) + return redirect('/') + return render(request, 'login.html') + +def user_logout(request): + logout(request) + return redirect('/') \ No newline at end of file diff --git a/blog/urls.py b/blog/urls.py index 3c47136..977d1c3 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -23,4 +23,7 @@ urlpatterns = [ path('', views.archive), re_path(r'^article/(?P\d+)$', views.get_article, name='get_article'), path('article/new/', views.create_post), + path('registration/', views.registration), + path('login/', views.login_page), + path('logout/', views.user_logout), ] diff --git a/db.sqlite3 b/db.sqlite3 index a898b659dad04d60a9b8e450ef7c3f0f4852f5e6..2b2d9e4671d35cc4cde72a2c8c3598b690326fa5 100644 GIT binary patch delta 1773 zcmaLYO>Em#902fm-qt2v=Fu`bG^TAV9YSG_pP#=HsiH}m?=IhIlUz1Cj_t%woW!y7 zamg&iX%|X`kdU}=n}jr#9V)?P(r%m(AaOuqT)81m+#qSzY3(Ra{`x=3)=%&M_y0Y3 zy7b`b(kEAu;qE(OWVnC-tLtxl;aI?u4(tysiG7Dz*hB1V>^|n(w^y+}pHIDw{p?tF z92;&IdL7}-zH-kr>OI#gibCY`n+me>2>uDG1f9a#C|vx^_NKXoG)w51=H7}uWj z`M&+kIkADu?XW)#F0Q)4qBH332&yQN_Y8$6X~qMH!{L$BU4;mRsZLJFMq~L=+^qMk zriUn`$zXzOcS6O{mM+)BikYqH1z(SdX$eaZN`5twEwp zAcIrfgn|@3Qv==*rRllRR7692bnHvH_C7X_n+T)_!|&Zd`M}ce-&hFs)djAZsAZ7lQuY*0$6UWY$A9 zdY09y%i(-@sECZeSoKEOfy&?A5`6It*yi$*3U{IeHO?I~9s?d z6&;}bI6W;m7T6ch%~dPzTc@iyoz|I5J4TbmNUdMcf_0Hk%brrU%~VuVlM*r;P8KUw z!q?N-Sh-W1gtqhST^(+fHw)YSezwG|n*&oH=o@NNl<=nbng}pB2Oyv*d-d#G^uK>@ z%*gS2xkeM2kO4WZ8848EQ1vwQOg^Iq;GK$yNwG<`N@6>imJ`onoa5oW&@>`u^KeH3BcJguVkakjb z!UX~pg;S6Qkg%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