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 @@
<a class="nav-link" href="{% url 'author_list' %}">Список авторов</a>
</li>
</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>
</nav>
\ No newline at end of file
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.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.views.decorators.csrf import csrf_protect
from django.views.generic import FormView, DeleteView
from articles.forms import SearchForm, ArticleForm, CommentForm
......@@ -89,5 +91,10 @@ class ArticleDeleteView(DeleteView):
confirm_deletion = False
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):
return self.delete(request, *args, **kwargs)
......@@ -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'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
DEBUG = True
ALLOWED_HOSTS = ['*']
......@@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'articles',
]
......@@ -48,7 +49,7 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# 'whitenoise.middleware.WhiteNoiseMiddleware',
]
ROOT_URLCONF = 'core.urls'
......@@ -101,7 +102,7 @@ AUTH_PASSWORD_VALIDATORS = [
},
]
LOGIN_URL='login'
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
......
......@@ -22,4 +22,5 @@ handler404 = error_404
urlpatterns = [
path('admin/', admin.site.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