Commit 8241ef34 authored by Борис Ким's avatar Борис Ким

Создал вью для входа пользователей и сделал форму

parent 2ddee60f
......@@ -3,4 +3,4 @@ from django.apps import AppConfig
class AccoutnsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accoutns'
name = 'accounts'
from django import forms
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="{% static 'fontawesomefree/css/all.min.css' %}" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<div class="container" style="margin: 200px;">
<div class="d-flex justify-content-center shadow-lg p-3 mb-5 bg-white rounded">
<form action="{% url 'accounts:login' %}" method="POST" style="width: 500px;">
{% csrf_token %}
{% if has_error %}
<h6 class="d-flex justify-content-center" style="margin: 20px; color: red;"> Error! Wrong username or password. </h5>
{% endif %}
<div class="form-group row">
<label for="inputLogin3" class="col-sm-2 col-form-label">Login</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputLogin3" name="username" placeholder="Username">
</div>
</div>
<div class="form-group row">
<label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" name="password" class="form-control" id="inputPassword3" placeholder="Password">
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button class="btn btn-primary">Sign in</button>
</div>
</div>
</form>
</div>
</body>
</html>
\ No newline at end of file
from django.urls import path
from accounts import views as accounts_views
app_name = "accounts"
urlpatterns = [
path('login/', accounts_views.LoginView.as_view(), name="login")
]
from django.conf import settings
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.views.generic import View, FormView
class LoginView(View):
def get(self, request, *args, **kwargs):
return render(request, 'auth/login.html')
def post(self, request, *args, **kwargs):
username = request.POST.get('username')
password = request.POST.get('password')
next_path = request.POST.get('next')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
if next_path:
return HttpResponseRedirect(next_path)
return redirect(settings.LOGIN_REDIRECT_URL)
return render(request, 'auth/login.html', {'has_error': True})
class LogoutView(View):
def post(self, request, *args, **kwargs):
logout(request)
return redirect(settings.LOGOUT_REDIRECT_URL)
\ No newline at end of file
from django.shortcuts import render
# Create your views here.
......@@ -30,6 +30,7 @@ INSTALLED_APPS = [
'webapp',
'crispy_forms',
'fontawesomefree',
'accounts'
]
MIDDLEWARE = [
......@@ -127,3 +128,6 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
\ No newline at end of file
"""portal URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
......@@ -20,4 +5,5 @@ from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('webapp.urls')),
path('accounts/', 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