The pre-requisite for having ‘user’ in the request instance is the 
‘django.contrib.auth.context_processors.auth’ in your TEMPLATES 
context_processors.

See the second answer (should be the accepted one as it has received far more 
community votes) in this stack overflow 
article<https://stackoverflow.com/a/1064621/1956886>.

Note that the settings referred to in that article are obsolete and have been 
replaced as per above. You can also find a link to the documentation in the 
article.

Also, you don’t need to explicitly get the username from the request object 
like this. Assuming you have all the required context processors present you 
can simply use {{ request.user.username }} within your template.

In any case, the best practice with Django template CBVs is not to override 
get() here, but override 
get_context_data()<https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-display/#adding-extra-context>
 where the request object is available as self.request.

/d

On 20201115, at 01:55, mike vickers 
<[email protected]<mailto:[email protected]>> wrote:

You would want to include that line in a method, not as a class attribute. For 
example:

def get(self, request):
        user = request.user.username
        ...
        return render(...


On Sat, Nov 14, 2020 at 9:37 AM Kasper Laudrup 
<[email protected]<mailto:[email protected]>> wrote:
Hi Barbara,

On 13/11/2020 22.46, Barbara Leonard wrote:
> make an attribute named "user"
>

How would that help? The attribute value should be set to the currently
logged in user. Just adding the attribute to request object will not do
that. Clearly something else is missing.

Kind regards,

Kasper Laudrup

--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:django-users%[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/93f48576-7bf0-b46d-9403-5f0c23f3babc%40stacktrace.dk.

--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACaDatT8mUT142AYWuw6wemqLMzbKjCvZyT%3DAqi-_WoZrYsn9Q%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CACaDatT8mUT142AYWuw6wemqLMzbKjCvZyT%3DAqi-_WoZrYsn9Q%40mail.gmail.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4E255B5C-414B-4475-A939-5EE9BBF1627C%40uniquode.io.

Reply via email to