[Django]-How to use Dynamic variables in Django base.html


This has nothing to do with being in base.html. Neither does it have anything to do with functions, as you are not calling a function. The issue is simply that you have not read the documentation on template syntax. In a template, both attribute access and dictionary lookup are done with “.”.

{{ request.session.user }} 

Although I don’t know why you want to use the session for this anyway, since the user is in request.user.


In Django you dont use list indexes in template code (like request.session[‘user’]), instead you type it as a “sort of” function like: request.session.user to get it. This is just how the template system works.

In your use-case it might be worth noting that Django has an alias for request.session.user that is merely called user. So if you e.g. wanted to get the username you would do: user.username or user.first_name for their first name. You can of course achieve the same with request.session.user.username



as far as i remember, there already is a {{ user }} available in Django templates, if you use the Django user auth system, so you could try to use {{ user.username }}.

You may also look at this thread. Hope this helps.


Leave a comment