Your view is called login
, so when you try to call the login function, it calls your view instead of the built-in one. Call your view something else.
Django: login page is stuck at logging the authenticated users in
-
30-08-2022 - |
Frage
I have a custom login page, here's the view:
def login(request):
email = ''
password = ''
state = "Please login below: "
form = AuthenticationForm(data=(request.POST or None))
if form.is_valid():
# Since the USERNAME_FIELD in custom-user is the email, that is what
# we expect as input to the username field of this form
email = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=email,password=password)
if user is not None and user.is_active:
login(request, user)
state = "You have been logged in"
else:
state = "Invalid login credentials"
t = loader.get_template('login.html')
c = RequestContext(request, {'state': state, 'form': form})
return HttpResponse(t.render(c))
The view is working, because when I put a user name and not an email, the error field is being raised ('The email is required to create this user'). But when I enter the correct email and password, it gives me an error that login() takes only one argument in. Although when I tried passing login 1 argument instead of 2 (login(request) vs login(request, user)), the page won't redirect or do anything at all. It shows that it's loading though, but nothing happens :/
Here are my models for user:
class CustomUserManager(BaseUserManager):
def create_user(self, email, first_name, last_name, password=None,
**extra_fields):
'''
Create a CustomUser with email, name, password and other extra fields
'''
now = timezone.now()
if not email:
raise ValueError('The email is required to create this user')
email = CustomUserManager.normalize_email(email)
cuser = self.model(email=email, first_name=first_name,
last_name=last_name, is_staff=False,
is_active=True, is_superuser=False,
date_joined=now, last_login=now,)
cuser.set_password(password)
cuser.save(using=self._db)
return cuser
def create_superuser(self, email, first_name, last_name, password=None,
**extra_fields):
u = self.create_user(email, first_name, last_name, password,
**extra_fields)
u.is_staff = True
u.is_active = True
u.is_superuser = True
u.save(using=self._db)
return u
class CustomUser(AbstractBaseUser, PermissionsMixin):
'''
Class implementing a custom user model. Includes basic django admin
permissions and can be used as a skeleton for other models.
Email is the unique identifier. Email, password and name are required
'''
email = models.EmailField(_('email'), max_length=254, unique=True,
validators=[validators.validate_email])
username = models.CharField(_('username'), max_length=30, blank=True)
first_name = models.CharField(_('first name'), max_length=45)
last_name = models.CharField(_('last name'), max_length=45)
is_staff = models.BooleanField(_('staff status'), default=False,
help_text=_('Determines if user can access the admin site'))
is_active = models.BooleanField(_('active'), default=True)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
def get_full_name(self):
'''
Returns the user's full name. This is the first name + last name
'''
full_name = "%s %s" % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
'''
Returns a short name for the user. This will just be the first name
'''
return self.first_name.strip()
I would really appreciate help in resolving this issue!
Thanks a lot in advance!
Lösung
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow