【发布时间】:2011-02-10 02:04:09
【问题描述】:
我正在开展一个项目,该项目需要一个评级系统,以便他们可以对房屋的质量进行评级。他们会从管理面板中进行操作,因此访问者不会对此进行评价。
例如,对于每个列表,他们可以从 1 到 5 星对其进行评分: 地点: 房间: 餐饮选择: 社区: 总体:
在我的 models.py 文件中,我有这样的设置:
Class Listing (models.Model):
...
RATING_CHOICES = (
(1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 5),
)
location_rating = models.DecimalField(choices = RATING_CHOICES, max_digits=3, decimal_places=2)
room_rating = models.DecimalField(choices = RATING_CHOICES, max_digits=3, decimal_places=2)
dining_options_rating = models.DecimalField(choices = RATING_CHOICES, max_digits=3, decimal_places=2)
community_rating = models.DecimalField(choices = RATING_CHOICES, max_digits=3, decimal_places=2)
recreation_rooms_rating = models.DecimalField(choices = RATING_CHOICES, max_digits=3, decimal_places=2)
def __unicode__(self):
return self.name
def get_avg_rating(self):
avg_rating = (self.location_rating + self.room_rating + self.recreation_rooms_rating + self.dining_options_rating + self.community_rating) / 5
return avg_rating
我打算用一点 CSS 来显示评分。仅仅将 room_rating 或 avg_rating 放在模板标签中是行不通的,所以我假设我必须在 views.py 中添加一些代码行。我已经检查了 Django 文档,但我仍然不确定如何去做。
这似乎应该很容易做到,但我不确定从哪里开始。
【问题讨论】:
-
对象的属性需要从它们所在的对象访问。它们也需要存在。
标签: django