我在验证登录时遇到问题。
我的代码:
class UsersPass(models.Model):
id_user = models.IntegerField(null=False,primary_key=True)
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)`
views.py
def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
print('haslo',password)
if user:
login(request, user)
return redirect('main_view')
else:
return HttpResponse("Account not active or invalid login details")
else:
return render(request, 'main.html', {})`
urls.py
path("",views.index,name='index'),
path("main/",views.main_view, name='main_view'),
path('login/', views.user_login, name='user_login'),
从数据库中获取的密码和登录名是正确的。显示正确密码的打印件。我没有在控制台警告,但登录功能不工作.每次登录结束不正确的消息“帐户不活跃或无效的登录信息”。
有什么问题吗?
我多次检查与数据库的连接,更改密码和登录名,无论我输入的数据是否正确,我都无法登录。
/edit
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
import psycopg2
class UsersPass(models.Model):
id_user = models.IntegerField(primary_key=True)
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)
def __str__(self):
return f'{self.username} - {self.password}'
class Calendar(models.Model):
date_plan = models.DateField()
description = models.CharField(max_length=125, null=True)
priority = models.BooleanField(default=False)
class Client(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
surname = models.CharField(max_length=255)
phone_number =
models.CharField(max_length=200,default='123456789')
def __str__(self):
return f'{self.id} - {self.name}'`
setting.py
`from pathlib import Path
import os
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Application',
'bootstrap5',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Insurance.urls'
CSRF_COOKIE_SECURE = True
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'Insurance.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'myApp',
'USER': 'postgres',
'PASSWORD': 'mypass',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME':
'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
STATIC_URL = 'static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'``
1条答案
按热度按时间u4vypkhs1#
如果密码和用户名是正确的,如你所说,问题是,该帐户是不活跃
解决这个问题的最好方法是通过Django shell访问User模型数据,如果这个特定用户的“is_active”为False,则将其设置为True,它应该可以工作