Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve SQL entries from Django login

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.

like image 565
xan Avatar asked May 11 '26 03:05

xan


1 Answers

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)
like image 120
Martijn Pieters Avatar answered May 13 '26 16:05

Martijn Pieters