[英]Django - How to filter a ListView by user without using CBV?
Is it possible to do this?是否有可能做到这一点? I've been looking for quite a while, but every solution I've seen involves subclassing ListView
which I don't want to do.我一直在寻找很长一段时间,但是我看到的每个解决方案都涉及我不想做的ListView
子类化。 I'm sure there's a way to filter results by user without having to resort to class-based views, I just can't seem to find good information on it, am I missing something?我确信有一种方法可以按用户过滤结果而不必求助于基于类的视图,我似乎无法找到关于它的好信息,我错过了什么吗?
I've tried a few things similar to this, but I don't think it's going to work the way I'm trying, and the only other way I've seen is with CBV:我已经尝试了一些与此类似的事情,但我认为它不会像我尝试的那样工作,我见过的唯一另一种方法是使用 CBV:
url(r'^$', ListView.as_view(queryset=Game.objects.filter(user=User.user), template_name = 'userprofile.html')),
When you send a request to view you have already instance of the current user in the request:当您发送查看请求时,您已经在请求中包含当前用户的实例:
views.py视图.py
def my_not_cb_view(request):
user = request.user
games = Game.objects.filter(user=User.user)
context = {'games': games, 'user': user}
render_to_response(request, 'user profile.html', context=context)
urls.py网址.py
url(r'^$', my_not_cb_view)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.