我目前正在使用allauth包,它扩展了用户模型以包括其他字段,比如关于我的字段。我想知道是否有任何类似于登录的方式,我可以使用@decorator来检查User.profile。代码如下所示,我认为它比我能解释得更好。
我正在尝试@user_passes_test(lambda u: u.profile.account_verified),它总是返回<bound method UserProfile.account_verified of <UserProfile>>
型号:
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
about_me = models.TextField(null=True, blank=True)
def account_verified(self):
"""
If the user is logged in and has verified hisser email address, return True,
otherwise return False
"""
if self.user.is_authenticated:
result = EmailAddress.objects.filter(email=self.user.email)
if len(result):
return result[0].verified
return False查看:
@user_passes_test(lambda u: u.profile.account_verified)
def index(request):
//logic in here发布于 2014-03-15 05:58:35
它返回一个绑定方法应该是一个巨大的提示:它是一个方法,而不是一个值。您通常调用方法来让它完成它的工作,所以您缺少的是调用它。
@user_passes_test(lambda u: u.profile.account_verified)如果lambda函数返回true的bool(function_result),则此测试通过:对于方法,它始终为true。
您想要的是调用该方法并让它返回一个true或false。
@user_passes_test(lambda u: u.profile.account_verified())或者,如果您希望该方法是一个属性,则使用@property装饰该方法。
@property
def account_verified(self):现在它是一种财产,你不需要叫它。
https://stackoverflow.com/questions/22420082
复制相似问题