我正在尝试在我的项目中实现 django-badges,并且我正在尝试授予 Autobigrapher 徽章以填写个人资料字段,但从未授予过徽章(已确认,因为在数据库中没有创建任何内容)。但是,如果我创建一个视图,该视图将使用 get_progress() 或 get_progress_percentage() 返回用户徽章的进度,则表明满足所需的条件。例如,我的测试视图如下所示:
#views.py
def badges(request, name):
user = get_object_or_404(User, username=name)
badge = Badge.objects.get(id='autobiographer')
progress = badge.meta_badge.get_progress(user)
progress_percentage = badge.meta_badge.get_progress_percentage(user)
return render('badges.html', {'badge': badge, 'progress': progress, 'progress_percent': progress_percent}, request=request)
#badge_template.html
{{ progress }}
<br />
{{ progress_percent }}
#meta_badges.py
class Autobiographer(badges.MetaBadge):
id = "autobiographer"
model = BaseProfile
one_time_only = True
title = "Autobiographer"
description = "Completed 5 User Profile Fields"
level = "1"
progress_start = 0
progress_finish = 5
def get_user(self, instance):
return instance.user
def get_progress(self, user):
has_avatar = 1 if user.get_profile().avatar else 0
has_city = 1 if user.get_profile().city else 0
has_twitter = 1 if user.get_profile().twitter else 0
has_facebook = 1 if user.get_profile().facebook else 0
has_linkedin = 1 if user.get_profile().linkedin else 0
has_google_plus = 1 if user.get_profile().google_plus else 0
has_tumblr = 1 if user.get_profile().tumblr else 0
return has_avatar + has_city + has_twitter + has_facebook + has_linkedin + has_google_plus + has_tumblr
def check_avatar(self, instance):
return instance.user.get_profile().avatar
def check_city(self, instance):
return instance.user.get_profile().city
def check_twitter(self, instance):
return instance.user.get_profile().twitter
def check_facebook(self, instance):
return instance.user.get_profile().facebook
def check_linkedin(self, instance):
return instance.user.get_profile().linkedin
def check_tumblr(self, instance):
return instance.user.get_profile().tumblr
def check_google_plus(self, instance):
return instance.user.get_profile().google_plus
如果 user1 填写了 5 个配置文件字段,example.com/badges/user1 看起来像:
5
100
任何人成功使用 django-badges 并知道我缺少什么,或者是否有另一种有效的徽章解决方案?它似乎很容易使用,但我无法让它工作。