I rewrote my app from scratch and decided to move forward with the rest of the training.
That’s why I wanted to tweak my index.html in the sense that when a User Logs In, he will automatically redirected to the home page and the menu will change from “Log In” to “Log Out” :
To add to and expand on @CodenameTim’s answer, you shared a view with us - but it’s the wrong view.
In the case of a successful login, you are redirecting to a url identified by “app_base:index”. It’s that view that should be rendering the template fragment you supplied above, and that’s the view we would need to see.
I changed both my login and index views in order to get more debug info :
def loginUser(request):
if request.method == "POST":
form = loginUserForm(request.POST)
if form.is_valid():
form_email = form.cleaned_data["email"]
form_password = form.cleaned_data["password"]
user = authenticate(request, username=form_email, password=form_password)
if user is not None:
print("USER is => " + str(user))
print("USER.IS_AUTHENTICATED => " + str(user.is_authenticated))
login(request, user)
return redirect("app_base:index")
else:
user = None
form = loginUserForm()
return render(request, "app_accounts/login.html", {"form":form})
Yep, I’m stumped at the moment - which means I need to try to recreate the issue locally.
I’m really glad you included that print in index, that’s illuminating.
I’d be really curious to see what is in request.session at that point too - and what request.user is. I’m beginning to believe that there’s something in the session middleware that isn’t pulling the user correctly - but no ideas yet what that might be.
Guys, I found a workaround, I just used “ModelBackend” instead of “BaseBackend” in my custom backends.py file :
from django.contrib.auth.backends import ModelBackend
from .models import User
class EmailBackend(ModelBackend):
def authenticate(self, request, username=None, password=None):
try:
user = User.objects.get(email=username)
return user
except User.DoesNotExist:
return None
So, my guess here is that the issue was coming from my custom UserManager, I don’t really understand why but I’d be grateful if someone would explain it
Without seeing the complete then-current versions of your custom manager, backend, and user model, it would be tough to explain. Browsing through the various classes show some tight interrelationships among them.
Just as one example, ModelBackend implements the following get_user method:
def get_user(self, user_id):
try:
user = UserModel._default_manager.get(pk=user_id)
except UserModel.DoesNotExist:
return None
return user if self.user_can_authenticate(user) else None
note how it’s not looking for objects as a specific manager, it’s looking for whatever the user model defines as the default manager.
(I’m not saying that this is in any way related to the issues you were facing. I’m only pointing out one example of the interrelationships existing between the backends and user model classes.)
So I think the only way to get the explanations you’re looking for would be to see the classes involved and follow the flow through the various components.