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
-- 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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8635848c-b7fc-4b6b-9b53-f6107ce42717n%40googlegroups.com. |
make an attribute named "user" On Fri, Nov 13, 2020 at 2:27 PM Apolo Machine <[hidden email]> wrote:
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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAcuwDfBL82t2WebGvYmUEiP%2BCD7tv3aBTL7f%2BrH0igzVUbSuQ%40mail.gmail.com. |
where? in my model service? i'm not quiet understand thanks for responding El vie., 13 nov. 2020 a las 18:53, Barbara Leonard (<[hidden email]>) escribió:
-- ::Apolo::
-- 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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALOuZTBP0S3fVSuuqw6v8sc2A67115iwNO_QKh_Ubw265g35DQ%40mail.gmail.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. On Fri, Nov 13, 2020 at 5:22 PM Apolo Machine <[hidden email]> wrote:
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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAcuwDdKBHWLQoWSmnJLx4MVHg4Jbju5VGPaACFfmrhNNf95bw%40mail.gmail.com. |
In reply to this post by Apolo Machine
Hi Apolo,
On 13/11/2020 19.19, Apolo Machine wrote: > 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 > 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 -- 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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d3ee16bf-eb6e-2c8b-a3f6-efc7237296d3%40stacktrace.dk. |
In reply to this post by Barbara Leonard
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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/93f48576-7bf0-b46d-9403-5f0c23f3babc%40stacktrace.dk. |
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 <[hidden email]> wrote: Hi Barbara, 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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACaDatT8mUT142AYWuw6wemqLMzbKjCvZyT%3DAqi-_WoZrYsn9Q%40mail.gmail.com. |
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
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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4E255B5C-414B-4475-A939-5EE9BBF1627C%40uniquode.io. |
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 El sáb., 14 nov. 2020 a las 19:59, David Nugent (<[hidden email]>) escribió:
-- ::Apolo::
-- 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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALOuZTD42NYQ6%3DsbV%3D0iYxT9eD5Afot65xZBJAmJs3UMSQWBzA%40mail.gmail.com. |
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, [hidden email] đã viết:
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 [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/31e7e12b-89a6-4647-80b6-669129bff10en%40googlegroups.com. |
Free forum by Nabble | Edit this page |