Вот мой код:
views.py
def auth(request):
if request.method == 'POST':
Form = AuthForm(request.POST)
if Form.is_valid():
name = request.POST.get('username')
password = request.POST.get('password')
init = Users.objects.get(name=name)
if init.name == name and init.password == init.hash_password(password):
request.session['name'] = name
user = authenticate(request, username=name, password=password)
if user is not None:
login(request, user)
return redirect('/')
else:
messages.error(request, 'Что то у вас неправильно')
else:
messages.error(request, 'Неправиильный юзернейм или пароль')
else:
messages.error(request, 'Форма заполнена неправильно')
else:
Form = AuthForm()
return render(request, 'auth.html', {'Form': Form})
models.py
from django.db import models
import hashlib
import secrets
class Users(models.Model):
name = models.TextField(unique=True)
email = models.TextField(unique=True)
profile_image = models.ImageField(upload_to='profile_photos', null=True)
verified = models.BooleanField(null=True, default=False)
unique_token = models.TextField(null=True)
password = models.TextField()
def generate_token(self):
hash_code = secrets.token_hex(16)
self.unique_token = hash_code
def enc_password(self):
enc_password = hashlib.sha256(bytes(self.password, 'utf-8'))
self.password = enc_password.hexdigest()
def hash_password(self, argument):
enc_password = hashlib.sha256(bytes(argument, 'utf-8'))
enc_password = enc_password.hexdigest()
return enc_password
def __str__(self):
return self.name
class Meta:
verbose_name = "user_info"
Пробовал логгировать и узнать ошибку
def auth(request):
if request.method == "POST":
Form = AuthForm(request.POST)
if Form.is_valid():
username = request.POST['username']
password = hash_password(request.POST['password'])
print(f'database url: {settings.DATABASES}')
print(f'auth backend: {settings.AUTHENTICATION_BACKENDS}')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/')
else:
return HttpResponse(user)
else:
messages.error(request, 'error')
else:
Form = AuthForm()
return render(request, 'auth.html', context={'Form': Form})
Вышло:
database url: {'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': WindowsPath('C:/Users/user/Desktop/Python/itforum/itforum/db.sqlite3'), 'ATOMIC_REQUESTS': False, 'AUTOCOMMIT': True, 'CONN_MAX_AGE': 0, 'CONN_HEALTH_CHECKS': False, 'OPTIONS': {}, 'TIME_ZONE': None, 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', 'TEST': {'CHARSET': None, 'COLLATION': None, 'MIGRATE': True, 'MIRROR': None, 'NAME': None}}}
auth backend: ['django.contrib.auth.backends.ModelBackend']
Как вы видите в юзер и пассворд ничего, мне бы хочется узнать почему