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:
Thanks to all of you.I finally get it work with thisdef get_initial(self):
initial = super(ServiceCreateView, self).get_initial()
initial = initial.copy()
initial['user'] = self.request.user.username
return initialEl sáb., 14 nov. 2020 a las 19:59, David Nugent (<dav...@uniquode.io>) escribió: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
On 20201115, at 01:55, mike vickers <vicker...@gmail.com> 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 <lau...@stacktrace.dk> 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 django-users...@googlegroups.com.
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 django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACaDatT8mUT142AYWuw6wemqLMzbKjCvZyT%3DAqi-_WoZrYsn9Q%40mail.gmail.com.
--::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 django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/31e7e12b-89a6-4647-80b6-669129bff10en%40googlegroups.com.
No comments:
Post a Comment