I was recycling some of my code from an old project (Django 1.7) to use it in a new one (Django 1.8), so I get this Error every time I want to Login
It was working well in Django 1.7
For my models I'm using an 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()
I made a custom Authentication
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)
In my command prompt I can actually see the user getting logged in (it brings me the name of the user)
According to the Django documentation:
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 (eg, 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/
What happens is your having a non Numeric PK for the User, and Django doesn't like that. I had the same problem.
As stated in the documentation you should create a User for each authentication (if it doesn't exists yet) and change the get_user method in your ClientAuthBackend back to:
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
After I changed that, it all works smooth.
This error comes from your Django Session. I solved it using by
- python manage.py shell
At the shell, I ran
> from django.contrib.sessions.models import Session
> Session.objects.all().delete()
you can check the created session by running
Assuming the userid = '2b1189a188b44ad18c35e113ac6ceead'
s = Session.objects.get(pk='2b1189a188b44ad18c35e113ac6ceead')
you can read more about session here
I hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.