0

I am just making a simple website as I am learning django.

I have a view function that looks like this:

  def vieworkout(request):
        date= datetime.today()
        obj = userworkouts.objects.filter(userid=52)
        date = request.POST['date']
        
        
        
        
        context = {
            'exercise': obj.exercise
        }
    
        return render(request, 'clientIndex.html', context)

when I use userworkouts.object.get() I can pass the exercise object no problem. When I change it to filter I get an error, "'QuerySet' object has no attribute 'exercise'". Does anyone know why this is happening?

1 Answers1

1

Because the .get returns a single object and the .filter returns a list like object. You have to grab the first result.

Try this:

obj = userworkouts.objects.filter(userid=52).first()

If you want to render the entire collection, don't use first and pass the entire list into your template:

def vieworkout(request): 
    workouts=userworkouts.objects.filter(userid=52)
    return render(request, 'clientIndex.html', { 'workouts': workouts })

In your template you can iterate through the workouts and dot into each object's properties.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86