How can I get information about the logged-in user in a Django application?

How can I get information about the logged-in user in a Django application?

What I want to do is get the user information from Logged-in user and put additional information and store in a database(models.py). So in views.py:

def registerView(request):
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login_url')
        
        else:
            form = UserCreationForm()
            return render(request, 'main/register.html',{'form':form})

here how do I grab the users information

Answer

There are several ways for get information from authenticated user in django

1) In the view

You can use request.user like this :

def test_user(request):
    if request.user.is_authenticated:
        # Do some stuff with the user = request.user
        my_user = request.user
        my_user.email = 'demo_user@yopmail.com'
        my_user.save()
    else:
        # The user is not authenticated
        # You can't do anything i guess
    return render(request, 'template/logged_in_user.html')

2) In the template

<!-- Show something to the authenticated user -->
{% if user.is_authenticated %}
<h1> Hi {{ user.name }} </h1>
{% else %}
<!-- Show something to the guess user -->
<h1> Hi Guess ! </h1>
{% endif %}