I have the following model
class Team(models.Model):
name = models.CharField(max_length=100)
members = models.ManyToManyField(User, related_name="members", blank=True, null=True)
And the following view (controller)
def my_teams(request):
my_list = Team.objects.filter(members=request.user).order_by('name')
return render_to_response('teams/index.html', {'my_list': my_list})
This works fantastic when a user is logged in, but I receive the following error while testing as an Anonymous user.
Exception Value: int() argument mus开发者_运维问答t be a string or a number, not 'AnonymousUser'
How do I cater for Anonymous users? Is this handled in my template or view?
Any advice on how to achieve this would be greatly appreciated.
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?
精彩评论