''AnonymousUser' object is not iterable at my index

So hello guys, Im new here at Django and while running my code i ran to this kind of error 'AnonymousUser' object is not iterable it highlight my error in my index line 37. patient=Patient.objects.filter(user = request.user)

so this is my views.py

def index(request):
feature1 = Feature.objects.all()
Appnt = Appointment()
if request.user:
    patient=Patient.objects.filter(user = request.user)
    if request.method == 'POST':
        name = request.POST.get('name')
        Appnt.name = name
        email = request.POST.get('email')
        Appnt.email = email
        phone = request.POST.get('phone')
        Appnt.phone = phone
        Adate = request.POST.get('date')
        Appnt.Adate=Adate
        Dept = request.POST.get('department')
        Appnt.Dept=Dept
        Doc = request.POST.get('doctor') 
        Appnt.Doc=Doc
        Content = request.POST.get('message') 
        Appnt.Content = Content
        Appnt.ref = "ref_" + str(len(Appointment.objects.all()) + 1)
        Appnt.save()
        sendEmail(email, "Date: " + Adate + "\n" + name + "\n" + phone + "\n" + Dept + "\n" + 
        Doc + "\n"+ Content)
    context = {
        'feature1' : feature1,
        'Appointment' : Appnt
    }
return render(request, 'index.html', context)

and this is my urls.py

path('', views.index , name='index'),


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source