我们可以将 django 过滤器与变量一起使用吗?
Can we use django filter with a variable?
这是我的代码:
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(无法 return 任何值)
def employeeView(request):
member = User.username
members = Members.objects.filter(username= member)
return render(request,'members/user_profile.html', {'members': members})
Views.py(return 正确)
def employeeView(request):
members = Members.objects.filter(username= 'mahesh')
return render(request,'members/user_profile.html', {'members': members})
我试图通过传递登录用户的用户名来过滤 Members.obejcts 来获取数据。它在硬编码时有效,但不是通过传递变量。有什么解决方法吗?
试试这个,member = request.user.username
这是我的代码:
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(无法 return 任何值)
def employeeView(request):
member = User.username
members = Members.objects.filter(username= member)
return render(request,'members/user_profile.html', {'members': members})
Views.py(return 正确)
def employeeView(request):
members = Members.objects.filter(username= 'mahesh')
return render(request,'members/user_profile.html', {'members': members})
我试图通过传递登录用户的用户名来过滤 Members.obejcts 来获取数据。它在硬编码时有效,但不是通过传递变量。有什么解决方法吗?
试试这个,member = request.user.username