Добавлена проверка логина и пароля #17

This commit is contained in:
Artur Galyamov 2022-12-20 10:20:29 +05:00
parent 34259d5977
commit 9655132f5e

View File

@ -2,14 +2,13 @@ import os
from json import JSONEncoder from json import JSONEncoder
import requests import requests
from django.http import HttpRequest from django.contrib.auth import authenticate
from django.http import HttpResponse from django.http import HttpRequest, HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse
from django.views import View from django.views import View
from django.views import View as BaseView
from cms.forms import ArticleForm from cms.forms import ArticleForm, UserForm
from cms.forms import UserForm
from cms.models import Article from cms.models import Article
@ -91,7 +90,7 @@ def new_article(request):
context=article_context) context=article_context)
class AuthenticationView(BaseView): class AuthenticationView(View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
user_form = UserForm() user_form = UserForm()
auth_context = { auth_context = {
@ -102,4 +101,11 @@ class AuthenticationView(BaseView):
context=auth_context) context=auth_context)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
return HttpResponse('ok') username = request.POST['username']
password = request.POST['password']
authenticated = authenticate(username=username,
password=password)
if authenticated:
return HttpResponseRedirect(reverse('new-article'))
else:
return HttpResponseRedirect(reverse('authenticate'))