1👍
✅
The Problem:
user = request.user.is_authenticated
makes the variable user = True
, so when you use it in
if request.user.is_authenticated and Profile.objects.get(applicant=user, status= 'Update'):
,
you are really doing the following:
if request.user.is_authenticated and Profile.objects.get(applicant=True, status= 'Update'):
So of course there will be no matches since no applicant equals True
.
The Solution
Change the two lines to:
user = request.user # Now user equals the actual user
if user.is_authenticated and Profile.objects.get(applicant=user, status= 'Update'):
On another note:
I hesitate to use a get
query without putting it in a try/except statement so that you can handle what should happen if a Profile
is not found:
user = request.user # Now user equals the actual user
if user.is_authenticated:
try:
profile = Profile.objects.get(applicant=user, status= 'Update')
except Profile.DoesNotExist:
# Code to handle what happens if a Profile is NOT found
# Maybe redirect to a page not found
# or maybe redirect to a page to create an applicant?
EDIT
def index(request):
user = request.user
if user.is_authenticated:
try:
profile = Profile.objects.get(applicant=user)
except Profile.DoesNotExist:
print("Profile Does Not Exist")
# Redirect to a Profile creating page?
else:
if profile.status == 'Update':
return redirect('user-profile-update')
else:
return redirect('user-profile')
context = {
'check_profile_update':check_profile_update,
}
return render(request, 'dashboard/index.html', context)
Edit 2
As per your comment, the final working version is then:
def index(request):
user = request.user
if user.is_authenticated:
try:
profile = Profile.objects.get(applicant=user)
except Profile.DoesNotExist:
print("Profile Does Not Exist")
# Redirect to a Profile creating page?
else:
if profile.surname == None:
return redirect('user-profile-update')
else:
return redirect('user-profile')
context = {
'check_profile_update':check_profile_update,
}
return render(request, 'dashboard/index.html', context)
Source:stackexchange.com