2👍
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.
2👍
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
- [Django]-Django regroup odd behavior
- [Django]-Moving project from one computer to another
- [Django]-How to get the length of a TextField in Django?
- [Django]-Why django contains a lot of '__init__.py'?
- [Django]-Django – initialize model fields including manytomanyfields
1👍
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.
- [Django]-How to use SASS in Django?
- [Django]-Django celery only calls 1 of 2 apply_async task
- [Django]-Django template and XML question