im a newbe, i'm trying to get the username in a view

41 views
Skip to first unread message

Apolo Machine

unread,
Nov 13, 2020, 2:28:39 PM11/13/20
to Django users
I'm trying to get the username within a view(CBV), the documentation I found says that the logged user is within the request, but when I do

class ServiceCreateView(LoginRequiredMixin, CreateView):
          model = Service
          form_class = ServiceForm
          user=request.user.username

i get this error

AttributeError: module 'django.http.request' has no attribute 'user

any help is appreciated

Barbara Leonard

unread,
Nov 13, 2020, 4:53:47 PM11/13/20
to django...@googlegroups.com
make an attribute named "user"

--
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 django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8635848c-b7fc-4b6b-9b53-f6107ce42717n%40googlegroups.com.

Apolo Machine

unread,
Nov 13, 2020, 5:23:53 PM11/13/20
to django...@googlegroups.com
where? in my model service?
i'm not quiet understand
thanks for responding

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/jW90KnHwaw0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAcuwDfBL82t2WebGvYmUEiP%2BCD7tv3aBTL7f%2BrH0igzVUbSuQ%40mail.gmail.com.


--
::Apolo::

Barbara Leonard

unread,
Nov 13, 2020, 10:27:18 PM11/13/20
to django...@googlegroups.com
Yes I believe so I did programming years ago but from the error code you are getting I would say try that because the syntex can not process what it does not have. It is telling you it do not have an attribute named user there it can not return such output, so yes try the module area to put in the user. I hope this helps.

Kasper Laudrup

unread,
Nov 14, 2020, 9:34:58 AM11/14/20
to django...@googlegroups.com
Hi Apolo,
The documentation for the user attribute here:

https://docs.djangoproject.com/en/3.1/ref/request-response/#attributes-set-by-middleware

says "Some of the middleware included in Django’s contrib apps set
attributes on the request. If you don’t see the attribute on a request,
be sure the appropriate middleware class is listed in MIDDLEWARE."

So maybe you should check your MIDDLEWARE settings?

Kind regards,

Kasper Laudrup

Kasper Laudrup

unread,
Nov 14, 2020, 9:37:28 AM11/14/20
to django...@googlegroups.com
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

mike vickers

unread,
Nov 14, 2020, 11:33:35 AM11/14/20
to django...@googlegroups.com
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(...


--
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 django-users...@googlegroups.com.

David Nugent

unread,
Nov 14, 2020, 6:00:34 PM11/14/20
to django...@googlegroups.com, Apolo Machine
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

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() where the request object is available as self.request.

/d

Apolo Machine

unread,
Nov 15, 2020, 7:24:50 AM11/15/20
to dav...@uniquode.io, django...@googlegroups.com
Thanks to all of you.
I finally get it work with this
def get_initial(self):
        initial = super(ServiceCreateView, self).get_initial()
        initial = initial.copy()
        initial['user'] = self.request.user.username
        return initial
--
::Apolo::

Bunh Tha Chau

unread,
Nov 15, 2020, 10:23:19 AM11/15/20
to Django users

        initial = super(ServiceCreateView, self).get_initial()
        initial = initial.copy()
        initial['user'] = self.request.user.username
        return initial


Vào lúc 19:24:50 UTC+7 ngày Chủ Nhật, 15 tháng 11, 2020, apolom...@gmail.com đã viết:
Reply all
Reply to author
Forward
0 new messages