[英]Django: How do I get the username of the logged-in user in my class based view?
I have implemented a DeleteView and would like it to redirect it to the logged-in user's profile after deleting the object:我已经实现了一个 DeleteView 并希望它在删除 object 后将其重定向到登录用户的配置文件:
class DeleteAttendanceFeedItem(DeleteView, LoginRequiredMixin):
model = AttendanceFeedItem
template_name = "users/delete_attendance_feed_item.html"
context_object_name = "attendance_feed_item"
success_url = reverse_lazy("user_profile", kwargs={"username" : request.user.username})
I don't have access to the request object in a class-based view, though.不过,我无法在基于类的视图中访问请求 object。 Searching StackOverflow suggests I have access to self.request.user.username, but that's throwing the same error.
搜索 StackOverflow 表明我可以访问 self.request.user.username,但这会引发相同的错误。
This seems like a very simple thing that I have burned a surprisingly large amount of time reading SO and Django documentation to absolutely no avail.这似乎是一件非常简单的事情,我花费了大量时间阅读 SO 和 Django 文档,但完全无济于事。 Help?
帮助?
Use self.request.user.username
to get the username inside the get_success_url()
method使用
self.request.user.username
在get_success_url()
方法中获取用户名
class DeleteAttendanceFeedItem(DeleteView, LoginRequiredMixin):
model = AttendanceFeedItem
template_name = "users/delete_attendance_feed_item.html"
context_object_name = "attendance_feed_item"
def get_success_url(self): return reverse_lazy("user_profile", kwargs={"username": self.request.user.username})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.