I'm trying to create a custom session-based login in Django using a custom user model and a custom login template.
For some reasons it worked at first but now the authenticate method from django.contrib.auth is not authenticating user. When it did work, the login and signup button were hidden.
users/urls.py
app_name = 'users'
urlpatterns = [
path('login/', login_user, name='login_user'),
path('logout/', logout_user, name='logout_user'),
]
users/views.py
app_name = 'users'
def login_user(request):
django_logout(request)
message = ''
if request.method == 'POST':
email = request.POST['email']
password = request.POST['password']
user = authenticate(request, email=email, password=password)
if user is not None:
django_login(request, user)
return redirect('homepage')
else:
message = "Log in failed!"
messages.error(request, message)
return redirect('users:login_user')
else:
return render(request, 'users/login.html')
@login_required(login_url='users/login/')
def logout_user(request):
django_logout(request)
templates/users/login.html
<form class="bg-white rounded-5 shadow-5-strong p-5" method="post" action="/">
{% csrf_token %}
<!-- Email input -->
<div class="form-outline mb-4">
<label class="form-label" for="form1Example1">Email address</label>
<input type="email" name="email" id="form1Example1" class="form-control" />
</div>
<!-- Password input -->
<div class="form-outline mb-4">
<label class="form-label" for="form1Example2">Password</label>
<input type="password" type="password" id="form1Example2" class="form-control" />
</div>
<!-- Submit button -->
<button type="submit" class="btn btn-primary btn-block">Sign in</button>
</form>
users/models.py
class CustomUserManager(BaseUserManager):
def create_superuser(self, email, password):
if email is None:
raise TypeError('Users should have an Email')
if password is None:
raise TypeError('Password should not be none')
user = self.create_user(email, password)
user.is_superuser = True
user.is_staff = True
if user.is_superuser is not True:
raise ValueError(
'Superuser must be assigned to is_staff=True.')
if user.is_staff is not True:
raise ValueError(
'Superuser must be assigned to is_superuser=True.')
user.save()
return user
def create_user(self, email, password):
if email is None:
raise TypeError('Users should have an Email')
if password is None:
raise TypeError('Users must have a password')
email = self.normalize_email(email)
user = self.model(email=email)
user.set_password(password)
user.save()
return user
AUTH_PROVIDERS = {'facebook': 'facebook', 'google': 'google',
'twitter': 'twitter', 'email': 'email'}
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=255, unique=True, db_index=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
about = models.TextField(_(
'about'), max_length=500, blank=True)
auth_provider = models.CharField(
max_length=255, blank=False,
null=False, default=AUTH_PROVIDERS.get('email'))
USERNAME_FIELD = 'email'
objects = CustomUserManager()
def __str__(self):
return self.email
My sign in button in homepage.html
<a href="{% url 'users:login_user' %}">
<button type="button" class="btn btn-link px-3 me-2">
Login
</button>
</a>
Could you show me the way to solve this? Thank you!
Change your form like this
<form class="bg-white rounded-5 shadow-5-strong p-5" method="post" action="{% url 'users:login_user' %}">
{% csrf_token %}
<!-- Email input -->
<div class="form-outline mb-4">
<label class="form-label" for="form1Example1">Email address</label>
<input type="email" name="email" id="form1Example1" class="form-control" />
</div>
<!-- Password input -->
<div class="form-outline mb-4">
<label class="form-label" for="form1Example2">Password</label>
<input type="password" name="password" type="password" id="form1Example2" class="form-control" />
</div>
<!-- Submit button -->
<button type="submit" class="btn btn-primary btn-block">Sign in</button>
</form>
MultiValueDictKeyError at /users/login/ 'password'
this is because you're trying to access password value from request like this request.POST['password']
but you've not set name to your input(password) that's why it is giving you an error