asf

parent ecddf82d
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'
from django.db import models
# Create your models here.
{% 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 rel="stylesheet" href="{% static 'style.css' %}">
<title>Document</title>
</head>
<body>
<div class="container">
<form action="{% url 'login' %}" method="post">
{% csrf_token %}
<label for="username">Логин:</label>
<input id="username" type="text" name="username">
<label for="pass">Пароль</label>
<input type="password" name="pass" id="pass">
<input type="submit" value="Войти">
</form>
</div>
</body>
</html>
\ No newline at end of file
from django.test import TestCase
# Create your tests here.
from django.urls import path
from .views import login_view, logout_view
urlpatterns = [
path('accounts/login', login_view, name='login'),
path('accounts/logout', logout_view, name='logout'),
]
\ No newline at end of file
from django.contrib.auth import (
authenticate, login, logout
)
from django.shortcuts import render, redirect
def logout_view(request):
logout(request)
return redirect('login')
def login_view(request):
context = {}
if request.method == 'POST':
username = request.POST.get("username")
password = request.POST.get("pass")
user = authenticate(
request,
username=username,
password=password
)
if user:
login(request, user)
return redirect('article_list')
else:
context['has_error'] = True
return render(
request=request,
template_name='login.html',
context=context
)
...@@ -10,6 +10,12 @@ ...@@ -10,6 +10,12 @@
<a class="nav-link" href="{% url 'author_list' %}">Список авторов</a> <a class="nav-link" href="{% url 'author_list' %}">Список авторов</a>
</li> </li>
</ul> </ul>
{{ user.is_authenticated }}
{% if not user.is_authenticated %}
<a class="btn btn-success" href="{% url 'login' %}">Войти</a>
{% else %}
<a class="btn btn-danger" href="{% url 'logout' %}">Выйти</a>
{% endif %}
</div> </div>
</div> </div>
</nav> </nav>
\ No newline at end of file
from urllib.parse import urlencode from urllib.parse import urlencode
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required
from django.db.models import Q from django.db.models import Q
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render, redirect
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.views.decorators.csrf import csrf_protect
from django.views.generic import FormView, DeleteView from django.views.generic import FormView, DeleteView
from articles.forms import SearchForm, ArticleForm, CommentForm from articles.forms import SearchForm, ArticleForm, CommentForm
...@@ -89,5 +91,10 @@ class ArticleDeleteView(DeleteView): ...@@ -89,5 +91,10 @@ class ArticleDeleteView(DeleteView):
confirm_deletion = False confirm_deletion = False
success_url = reverse_lazy('article_list') success_url = reverse_lazy('article_list')
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return redirect('login')
return super().dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
return self.delete(request, *args, **kwargs) return self.delete(request, *args, **kwargs)
\ No newline at end of file
...@@ -23,7 +23,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent ...@@ -23,7 +23,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'django-insecure-wse7x-3t6%mo#l9t3r_c2_5ek_c$npk1lj31!*(lt9xtq!t5ea' SECRET_KEY = 'django-insecure-wse7x-3t6%mo#l9t3r_c2_5ek_c$npk1lj31!*(lt9xtq!t5ea'
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False DEBUG = True
ALLOWED_HOSTS = ['*'] ALLOWED_HOSTS = ['*']
...@@ -37,6 +37,7 @@ INSTALLED_APPS = [ ...@@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'accounts',
'articles', 'articles',
] ]
...@@ -48,7 +49,7 @@ MIDDLEWARE = [ ...@@ -48,7 +49,7 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # 'whitenoise.middleware.WhiteNoiseMiddleware',
] ]
ROOT_URLCONF = 'core.urls' ROOT_URLCONF = 'core.urls'
...@@ -101,7 +102,7 @@ AUTH_PASSWORD_VALIDATORS = [ ...@@ -101,7 +102,7 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
LOGIN_URL='login'
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/ # https://docs.djangoproject.com/en/3.2/topics/i18n/
......
...@@ -22,4 +22,5 @@ handler404 = error_404 ...@@ -22,4 +22,5 @@ handler404 = error_404
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', include('articles.urls')), path('', include('articles.urls')),
path('', include('accounts.urls')),
] ]
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