how to pass the current user in reverse_lazy()

61 views
Skip to first unread message

Saloni Kalra

unread,
Jun 24, 2018, 2:13:46 AM6/24/18
to django...@googlegroups.com
DeleteView:

class DeletePost(LoginRequiredMixin, SelectRelatedMixin, generic.DeleteView):
    username = self.object.user.username
    model = models.Post
    select_related = ("user","teacher","subject")
    success_url = reverse_lazy("posts:for_user", kwargs={'username': self.request.user.username},)

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(user_id=self.request.user.id)

    def delete(self, *args, **kwargs):
        messages.success(self.request, "Post Deleted")
        return super().delete(*args, **kwargs)

Error in terminal:

username = self.object.user.username
NameError: name 'self' is not defined

Daniel Ale

unread,
Jun 24, 2018, 8:14:31 AM6/24/18
to django...@googlegroups.com
Override the get_success_url(self) instead... That will work 

--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMO5YAqQCfk6P2r2J4e3XZz3Cznvbh970DArbrt33ZbpTEWbXA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Saloni Kalra

unread,
Jun 24, 2018, 8:18:01 AM6/24/18
to Django users
Yes it did! Thanks much!

Daniel Ale

unread,
Jun 24, 2018, 8:20:22 AM6/24/18
to django...@googlegroups.com

Saloni Kalra

unread,
Jun 24, 2018, 8:25:17 AM6/24/18
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages