How to cater for 'AnonymousUser' when filtering based on user

半腔热情 提交于 2019-12-05 16:02:11

I think I can solve this by modifying my view to be something like:

def my_teams(request):
    if request.user.is_authenticated():
        my_list = Team.objects.filter(members=request.user).order_by('name')
        return render_to_response('teams/index.html', {'my_list': my_list})
    else:
        return render_to_response('teams/index.html', {})

Is this best practice to return nothing? How would I now handle this in my template?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!