What is the right redirection in Django?
Question:
I’m a beginner in Django and I want to display the username from the nav bar. I did not use User authentication. I only created custom database table called Jobseeker:
class Jobseeker(models.Model):
username = models.CharField(max_length=50)
email = models.CharField(max_length=50)
password = models.CharField(max_length=50)
repassword = models.CharField(max_length=50)
def __str__(self):
return self.username
Because the criteria is the user should be able to login using username or email then password. I was able to achieve that using this code,
def login(request):
jobseeker = None
if request.method == 'POST':
userinput = request.POST['username']
try:
username = Jobseeker.objects.get(email=userinput).username
except Jobseeker.DoesNotExist:
username = request.POST['username']
password = request.POST['password']
jobseeker = auth.authenticate(username=username, password=password)
if Jobseeker is not None:
auth.login(request, jobseeker)
messages.success(request,"Login successfull")
return redirect('home')
else:
messages.error(request,'Invalid credentials, Please check username/email or password. ')
return render(request, 'login.html',)
Now my problem is using {{request.user}} in the nav bar it display the superuser’s username, instead on the jobseeker username. Superuser’s username. Code for the HTML
I want to display the username that i inputed in the login form. It should be one of these username that needs to be displayed
sorry for the bad english but hopefully it makes sense.
Answers:
You didn’t give me enough informations.
Up to now, I can only explain why the problem occurs when you login with email: it is because auth.authenticate
function is only called when the code in try
raise an error.
So you should fix like this:
jobseeker = None
if request.method == 'POST':
userinput = request.POST['username']
try:
username = Jobseeker.objects.get(email=userinput).username
except Jobseeker.DoesNotExist:
username = request.POST['username']
password = request.POST['password']
jobseeker = auth.authenticate(username=username, password=password)
if jobseeker is not None:
auth.login(request, jobseeker)
messages.success(request,"Login successfull")
return redirect('home')
else:
messages.error(request,'Invalid credentials, Please check username/email or password. ')
return render(request, 'login.html',)
If it still works badly, please comment to my answer and I’ll help you more.
I’m a beginner in Django and I want to display the username from the nav bar. I did not use User authentication. I only created custom database table called Jobseeker:
class Jobseeker(models.Model):
username = models.CharField(max_length=50)
email = models.CharField(max_length=50)
password = models.CharField(max_length=50)
repassword = models.CharField(max_length=50)
def __str__(self):
return self.username
Because the criteria is the user should be able to login using username or email then password. I was able to achieve that using this code,
def login(request):
jobseeker = None
if request.method == 'POST':
userinput = request.POST['username']
try:
username = Jobseeker.objects.get(email=userinput).username
except Jobseeker.DoesNotExist:
username = request.POST['username']
password = request.POST['password']
jobseeker = auth.authenticate(username=username, password=password)
if Jobseeker is not None:
auth.login(request, jobseeker)
messages.success(request,"Login successfull")
return redirect('home')
else:
messages.error(request,'Invalid credentials, Please check username/email or password. ')
return render(request, 'login.html',)
Now my problem is using {{request.user}} in the nav bar it display the superuser’s username, instead on the jobseeker username. Superuser’s username. Code for the HTML
I want to display the username that i inputed in the login form. It should be one of these username that needs to be displayed
sorry for the bad english but hopefully it makes sense.
You didn’t give me enough informations.
Up to now, I can only explain why the problem occurs when you login with email: it is because auth.authenticate
function is only called when the code in try
raise an error.
So you should fix like this:
jobseeker = None
if request.method == 'POST':
userinput = request.POST['username']
try:
username = Jobseeker.objects.get(email=userinput).username
except Jobseeker.DoesNotExist:
username = request.POST['username']
password = request.POST['password']
jobseeker = auth.authenticate(username=username, password=password)
if jobseeker is not None:
auth.login(request, jobseeker)
messages.success(request,"Login successfull")
return redirect('home')
else:
messages.error(request,'Invalid credentials, Please check username/email or password. ')
return render(request, 'login.html',)
If it still works badly, please comment to my answer and I’ll help you more.