Commit 34edf14d authored by Давид Ли's avatar Давид Ли

lesson 58

parent fdd4cb11
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
class RegisterForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = get_user_model()
fields = (
'username', 'password1',
'password2', 'first_name',
'last_name', 'email',
)
# Generated by Django 3.2.23 on 2024-02-22 13:52
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]
from django.db import models from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(AbstractUser):
pass
from accounts.views import RegisterView
from django.urls import path from django.urls import path
from django.contrib.auth.views import LoginView, LogoutView from django.contrib.auth.views import LoginView, LogoutView
...@@ -5,4 +7,5 @@ from django.contrib.auth.views import LoginView, LogoutView ...@@ -5,4 +7,5 @@ from django.contrib.auth.views import LoginView, LogoutView
urlpatterns = [ urlpatterns = [
path('login', LoginView.as_view(template_name='auth/login.html'), name='login'), path('login', LoginView.as_view(template_name='auth/login.html'), name='login'),
path('logout', LogoutView.as_view(), name='logout'), path('logout', LogoutView.as_view(), name='logout'),
path('register', RegisterView.as_view(), name='register'),
] ]
from django.contrib.auth import authenticate, login, logout from django.forms import BaseModelForm
from django.contrib.auth.decorators import login_required from django.http import HttpResponse
from django.shortcuts import redirect, render from accounts import forms
# YAGNI - you ain't gonna need it from django.views import generic
# KISS - keep it simple stupid from django.contrib.auth import login, get_user_model
@login_required from django.shortcuts import redirect, render
def login_view(request):
context = {}
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user:
login(request, user)
return redirect('index')
context['has_errors'] = True class RegisterView(generic.CreateView):
model = get_user_model()
template_name = 'auth/register.html'
form_class = forms.RegisterForm
return render(request, 'auth/login.html', context=context) def form_valid(self, form):
user = form.save()
login(self.request, user)
return redirect(self.get_success_url())
def logout_view(request): def get_success_url(self):
logout(request) return self.request.POST.get(
return redirect('index') 'next',
self.request.GET.get('next', '')
)
...@@ -89,18 +89,18 @@ DATABASES = { ...@@ -89,18 +89,18 @@ DATABASES = {
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [ AUTH_PASSWORD_VALIDATORS = [
{ # {
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', # 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
}, # },
{ # {
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', # 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
}, # },
{ # {
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', # 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
}, # },
{ # {
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', # 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
}, # },
] ]
...@@ -135,3 +135,5 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' ...@@ -135,3 +135,5 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_REDIRECT_URL = LOGOUT_REDIRECT_URL = 'index' LOGIN_REDIRECT_URL = LOGOUT_REDIRECT_URL = 'index'
LOGIN_URL = 'login' LOGIN_URL = 'login'
AUTH_USER_MODEL = 'accounts.User'
...@@ -21,22 +21,7 @@ from webapp import views ...@@ -21,22 +21,7 @@ from webapp import views
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', views.IndexRedirectView.as_view(), name='redirect_to_index'), path('', views.IndexRedirectView.as_view(), name='redirect_to_index'),
path('articles', views.IndexListView.as_view(), name='index'),
path('articles/add', views.ArticleCreateView.as_view(), name='articles_add'),
path('articles/<int:id>', views.ArticleDetailView.as_view(), name='articles_detail'),
path('articles/<int:id>/update', views.ArticleUpdateView.as_view(), name='articles_update'),
path('articles/<int:id>/delete', views.ArticleDeleteView.as_view(), name='articles_delete'),
path('articles/<int:article_id>/comments/add', views.CommentCreateView.as_view(), name='comments_add'),
path(
'articles/<int:article_id>/comments/<int:comment_id>/update',
views.CommentUpdateView.as_view(),
name='comments_update',
),
path(
'articles/<int:article_id>/comments/<int:comment_id>/delete',
views.CommentDeleteView.as_view(),
name='comments_delete',
),
path('articles/', include('webapp.urls')),
path('accounts/', include('accounts.urls')), path('accounts/', include('accounts.urls')),
] ]
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
{% block content %} {% block content %}
<h1>Вход</h1> <div class="d-flex justify-content-center flex-column align-items-center">
<h1 class="text-center mb-5">Вход</h1>
<div class="d-flex justify-content-center">
{% for error in form.non_field_errors %} {% for error in form.non_field_errors %}
<div class="alert alert-danger">{{ error }}</div> <div class="alert alert-danger">{{ error }}</div>
{% endfor %} {% endfor %}
...@@ -35,6 +35,12 @@ ...@@ -35,6 +35,12 @@
<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" class="btn btn-primary">Submit</button>
</form> </form>
<div class="mt-5">
Don't have account?
<a class="" href="{% url 'register' %}?next={{ request.get_full_path }}">Register</a>
</div>
</div> </div>
{% endblock %} {% endblock %}
{% extends 'base.html' %}
{% block content %}
<h1 class="text-center">Registration</h1>
{% include 'partial/form.html' with request_method='post' btn_text='Register' %}
{% endblock %}
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
{% if user.is_authenticated %} {% if user.is_authenticated %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{% url 'logout' %}">Logout</a> <a class="nav-link" href="{% url 'logout' %}?next={{ request.get_full_path }}">Logout</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<span class="nav-link fw-bold">Hello, {{ user.username }}</span> <span class="nav-link fw-bold">Hello, {{ user.username }}</span>
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
{% else %} {% else %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Login</a> <a class="nav-link" href="{% url 'login' %}?next={{ request.get_full_path }}">Login</a>
</li> </li>
{% endif %} {% endif %}
......
...@@ -12,7 +12,7 @@ class ArticleForm(forms.ModelForm): ...@@ -12,7 +12,7 @@ class ArticleForm(forms.ModelForm):
widgets = { widgets = {
'title': widgets.TextInput(attrs={ 'title': widgets.TextInput(attrs={
'class': 'form-control mb-3', 'class': 'form-control mb-3',
'placeholder': 'Author', 'placeholder': 'Title',
}), }),
'text': widgets.Textarea(attrs={ 'text': widgets.Textarea(attrs={
'class': 'form-control mb-3', 'class': 'form-control mb-3',
......
# Generated by Django 3.2.23 on 2024-02-22 13:52
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('webapp', '0008_auto_20240125_1404'),
]
operations = [
migrations.AlterField(
model_name='article',
name='title',
field=models.CharField(max_length=200, validators=[django.core.validators.MinLengthValidator(12)], verbose_name='Заголовок'),
),
]
from django.urls import path
from webapp import views
urlpatterns = [
path('', views.IndexListView.as_view(), name='index'),
path('add', views.ArticleCreateView.as_view(), name='articles_add'),
path('<int:id>', views.ArticleDetailView.as_view(), name='articles_detail'),
path('<int:id>/update', views.ArticleUpdateView.as_view(), name='articles_update'),
path('<int:id>/delete', views.ArticleDeleteView.as_view(), name='articles_delete'),
path('<int:article_id>/comments/add', views.CommentCreateView.as_view(), name='comments_add'),
path(
'<int:article_id>/comments/<int:comment_id>/update',
views.CommentUpdateView.as_view(),
name='comments_update',
),
path(
'<int:article_id>/comments/<int:comment_id>/delete',
views.CommentDeleteView.as_view(),
name='comments_delete',
),
]
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