asdf

parent 4dc0a149
from django import forms
from django.contrib.auth.forms import UserCreationForm, UsernameField
from django.contrib.auth.models import User
from django.forms import ModelForm
class RegisterForm(ModelForm):
password = forms.CharField(
label="Пароль",
required=True,
strip=False,
widget=forms.PasswordInput
)
password_confirm = forms.CharField(
label="Подтвердите пароль",
required=True,
strip=False,
widget=forms.PasswordInput
)
def clean(self):
cleaned_data = super().clean()
password = cleaned_data.get('password')
password_confirm = cleaned_data.get('password_confirm')
if password and password_confirm and password != password_confirm:
raise forms.ValidationError("Пароли не совпадают")
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data.get('password'))
if commit:
user.save()
return user
class Meta:
model = User
fields = [
'username',
'password',
'password_confirm',
'first_name',
'last_name', 'email']
class MyRegisterForm(UserCreationForm):
class Meta:
model = User
fields = [
'username',
'password1',
'password2',
'first_name',
'last_name', 'email']
field_classes = {'username': UsernameField}
...@@ -7,12 +7,15 @@ ...@@ -7,12 +7,15 @@
<meta name="viewport" <meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'style.css' %}"> <link rel="stylesheet" href="{% static 'style.css' %}">
<title>Document</title> <title>Document</title>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<form action="{% url 'login' %}" method="post">
<form action="{% url 'login' %}?next={{ request.GET.next }}" method="post">
{% csrf_token %} {% csrf_token %}
<label for="username">Логин:</label> <label for="username">Логин:</label>
<input id="username" type="text" name="username"> <input id="username" type="text" name="username">
......
{% load static %}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'style.css' %}">
<title>Document</title>
</head>
<body>
<div class="container">
{{ form.errors }}
<form action="{% url 'register' %}?next={{ request.GET.next }}" method="post">
{% csrf_token %}
<label for="username">Логин:</label>
<input id="username" type="text" name="username">
<label for="password">Пароль</label>
<input type="password" name="password1" id="password">
<label for="password_confirm">Подтверждение</label>
<input type="password" name="password2" id="password_confirm">
<label for="first_name">Имя</label>
<input type="text" name="first_name" id="first_name">
<label for="last_name">Фамилия</label>
<input type="text" name="last_name" id="last_name">
<label for="email">Почта</label>
<input type="email" name="email" id="email">
<input type="submit" value="Регистрация">
</form>
</div>
</body>
</html>
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import login_view, logout_view from .views import login_view, logout_view, register_view
urlpatterns = [ urlpatterns = [
path('accounts/login', login_view, name='login'), path('accounts/login', login_view, name='login'),
path('accounts/logout', logout_view, name='logout'), path('accounts/logout', logout_view, name='logout'),
path('accounts/create/', register_view, name="register")
] ]
\ No newline at end of file
from django.contrib.auth import ( from django.contrib.auth import (
authenticate, login, logout authenticate, login, logout
) )
from django.core.exceptions import ValidationError
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from .forms import RegisterForm, MyRegisterForm
def logout_view(request): def logout_view(request):
logout(request) logout(request)
next = request.GET.get('next')
if next:
return redirect(next)
return redirect('login') return redirect('login')
def login_view(request):
def login_view(request):
context = {} context = {}
print(request.GET)
if request.method == 'POST': if request.method == 'POST':
username = request.POST.get("username") username = request.POST.get("username")
password = request.POST.get("pass") password = request.POST.get("pass")
...@@ -21,6 +27,9 @@ def login_view(request): ...@@ -21,6 +27,9 @@ def login_view(request):
) )
if user: if user:
login(request, user) login(request, user)
next = request.GET.get('next')
if next:
return redirect(next)
return redirect('article_list') return redirect('article_list')
else: else:
context['has_error'] = True context['has_error'] = True
...@@ -29,3 +38,20 @@ def login_view(request): ...@@ -29,3 +38,20 @@ def login_view(request):
template_name='login.html', template_name='login.html',
context=context context=context
) )
def register_view(request, *args, **kwargs):
if request.method == "POST":
try:
form = MyRegisterForm(data=request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('article_list')
except ValidationError as e:
print(e)
else:
form = MyRegisterForm()
return render(request, template_name='register.html', context={"form": form})
...@@ -12,9 +12,10 @@ ...@@ -12,9 +12,10 @@
</ul> </ul>
{{ user.is_authenticated }} {{ user.is_authenticated }}
{% if not user.is_authenticated %} {% if not user.is_authenticated %}
<a class="btn btn-success" href="{% url 'login' %}">Войти</a> <a class="btn btn-success" href="{% url 'login' %}?next={{ request.get_full_path }}">Войти</a>
<a class="btn btn-success" href="{% url 'register' %}?next={{ request.get_full_path }}">Регистрация</a>
{% else %} {% else %}
<a class="btn btn-danger" href="{% url 'logout' %}">Выйти</a> <a class="btn btn-danger" href="{% url 'logout' %}?next={{ request.get_full_path }}">Выйти</a>
{% endif %} {% endif %}
</div> </div>
</div> </div>
......
...@@ -19,6 +19,7 @@ from .views.author_views import ( ...@@ -19,6 +19,7 @@ from .views.author_views import (
AuthorCreateView AuthorCreateView
) )
comments_routes = [ comments_routes = [
path('articles/<int:pk>/comment/add', path('articles/<int:pk>/comment/add',
CommentCreateView.as_view(), CommentCreateView.as_view(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment