gpt4 book ai didi

python - Django - 验证错误 "value must be an integer"

转载 作者:行者123 更新时间:2023-12-01 03:12:07 37 4
gpt4 key购买 nike

我从旧项目(Django 1.7)中回收了一些代码在新的(Django 1.8)中使用它,所以每次我想登录时都会收到此错误

enter image description here

它在 Django 1.7 中运行良好

对于我的模型,我使用 AbstractBaseUser

models.py

class Student(AbstractBaseUser, models.Model):
email = models.EmailField(max_length=50)
user_id = models.CharField(max_length=8, null=False, editable=False, default=id_generator, primary_key=True)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
prom_code = models.CharField(max_length=8, null=False, default="")
gender = (("M","Male"),("F","Female"),)
gender = models.CharField(max_length=1, choices=gender, default="M", null=False)
USERNAME_FIELD = 'email'

@property
def is_superuser(self):
return False

def has_perm(self, perm, obj=None):
return False

def has_module_perms(self, app_label):
return False

@property
def is_staff(self):
return False

def save(self, **kwargs):
if not self.user_id:
self.user_id = id_generator()
while Student.objects.filter(user_id = self.user_id).exists():
self.user_id = id_generator()

super(Student, self).save()

我进行了自定义身份验证

auth_backend.py

from registration_app.models import Student
import md5


class ClientAuthBackend(object):

def authenticate(self, username=None, password=None):
try:
user = Student.objects.get(email=username)
password_ver = md5.new(password).hexdigest()
if user.check_password(password_ver):
return user
else:
print("Entre aqui")
return None
except Student.DoesNotExist:
print("Entre aqui2")
return None

def get_user(self, user_id):
try:
user = Student.objects.get(user_id=user_id)
if user is not None:
return user
except Student.DoesNotExist:
return None

views.py

def user_login(request):
args = {}
form = LoginForm(request.POST)
username = request.POST.get('email', '')
password = request.POST.get('password', '')
user = authenticate(username=username, password=password)

if user is not None:
print user.first_name + " ah iniciado session"
login(request, user) #-----**Here is where i get the problem**-----
return HttpResponseRedirect('/user/')
else:
print("El usuario no existe")

args['form'] = form
return render(request, 'user_student/login.html', args)

在我的命令提示符中,我实际上可以看到用户正在登录(它给我带来了用户的名称)

最佳答案

根据 Django 文档:

The Django admin is tightly coupled to the Django User object. The best way to deal with this is to create a Django User object for each user that exists for your backend (e.g., in your LDAP directory, your external SQL database, etc.) You can either write a script to do this in advance, or your authenticate method can do it the first time a user logs in.

https://docs.djangoproject.com/en/1.11/topics/auth/customizing/

发生的情况是您为用户设置了非数字 PK,而 Django 不喜欢这样。我也遇到了同样的问题。

如文档中所述,您应该为每个身份验证创建一个User(如果尚不存在),并更改 ClientAuthBackend 中的 get_user 方法返回:

def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

更改后,一切顺利。

关于python - Django - 验证错误 "value must be an integer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42796563/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com