pythondjangovariablesfilterhardcode

Can we use django filter with a variable?


Here are my codes:

Models.py

class Members(models.Model):
      first_name = models.CharField(max_length=100)
      last_name = models.CharField(max_length=100) 
      username = models.CharField(max_length=100)
      email = models.EmailField()
      phone = models.CharField(max_length=14)

Views.py (fails to return any value)

def employeeView(request):
    member = User.username
    members = Members.objects.filter(username= member)
    return render(request,'members/user_profile.html', {'members': members})

Views.py (returns properly)

def employeeView(request):
    members = Members.objects.filter(username= 'mahesh')
    return render(request,'members/user_profile.html', {'members': members})

I am trying to get the data out by filtering the Members.obejcts by passing the username of the logged-in user. It works when hardcoded, but not by passing a variable. Is there any workaround?


Solution

  • try this instead, member = request.user.username