I'm new to Django. I wrote a login script using the built-in User models.
def login_user(request):
state = "Please login below..."
username = password = ''
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
return render_to_response('home.html',{'username': username})
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response('index.html',{'state':state})
This opens a new page home where I display the {{username}}.
However, how can I retrieve other values like id,email,fullname etc. of that particular username from database and display them on my home page. I've tried using User.objects.all(), but it doesn't seem to work.
You already have the user object, (authenticate() returns it), so just retrieve information from user. See the User documentation to see what you can get from it:
# ...
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
state = "You're successfully logged in!"
info = dict(username=username, email=user.email, fullname=user.get_full_name())
return render_to_response('home.html', info)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With