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 a898b65..2b2d9e4 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ