Space
Space

Reputation: 213

How to pass variable from view page to base template

I am trying to login the user into his profile and need to pass the username to a navbar in a base template. I have base.html(base template) and index-client.html(body template). Now from login view, i would like to pass username value to base.html.

Code after user authentication is:

        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('index-client.html')

I need to pass username to base.html i.e. a base template.

Upvotes: 0

Views: 502

Answers (2)

drew
drew

Reputation: 781

The {{ request.user }} method is built in with Django, and you can use this in any template to access the User object.

If you want to display the user's name in the navbar you can use:

{{ request.user.first_name|capfirst }} {{ request.user.last_name|capfirst }}

As can be seen this has the use of a template tag |capfirst to capitalise the first letter.

Upvotes: 1

itzMEonTV
itzMEonTV

Reputation: 20339

according to doc, redirect will accept

  • A model: the model’s get_absolute_url() function will be called.

  • A view name, possibly with arguments: reverse() will be used to reverse-resolve the name.

  • An absolute or relative URL, which will be used as-is for the redirect location.

So redirect to specific view which render the base html. In html

{% if request.user.is_authenticated %}
    {{ request.user.username }}
{% endif %}

Upvotes: 0

Related Questions