我在views.py中有这个:
user_list = User.objects.select_related().annotate(rating=Avg('userrating__rating')).order_by('-rating')[:5]
Run Code Online (Sandbox Code Playgroud)
而且我想围绕Avg,所以我有一个很好的整数来评分.
但是,如果我使用 int(Avg('userrating__rating')))
它说:
int()参数必须是字符串或数字,而不是'Avg'
如果我用round(Avg('userrating__rating'))它说:
需要/ a float的TypeError
相同的math.ceil或math.floor
看起来像是一个直接的事情,但我不知道该怎么做.谢谢.
我希望这个程序在每次单击"生成数字"按钮时返回一组新的数字,即0到10之间的6个整数.
但是,即使再次单击按钮,它也会返回相同的数字集.
这是生成数字的Model类代码:
public class Number {
int[] num = new int[6];
Random ran = new Random(10);
public int[] generate(){
for(int i = 0; i<6; i++)
{
num[i] = ran.nextInt(10);
}
return num;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是从该ActionListener方法调用此代码的方法:
public void actionPerformed(ActionEvent e) {
Number numb = new Number();
lot_num.setText(Arrays.toString(numb.generate()));
}
Run Code Online (Sandbox Code Playgroud)
我也无法告诉如何管理结果的格式,以便它通过文本字段间隔而不用逗号和括号.
我想显示评分最高的 5 位用户。然而,它们目前没有显示有序,当我向用户添加两个评级时,它会出现两次,而不是计算平均值并放在正确的位置。
这些是模型:
# this is model for user
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
profilepic = models.ImageField(blank=True)
city = models.ForeignKey(City)
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
# Uncomment if you don't want the slug to change every time the name changes
self.slug = slugify(self.user.username)
super(UserProfile, self).save(*args, **kwargs)
def __unicode__(self):
return self.user.username
# this is the model for user ratings - one to many relationship with User
class UserRating(models.Model):
user = models.ForeignKey(User)
comment = models.CharField(max_length=500)
rating = models.IntegerField(default=5) …Run Code Online (Sandbox Code Playgroud) 我要自定义主页上的问候语,因此它是:
登录并输入名字时:
约翰!你想去哪里?
其他:
你想去哪里?
这是我的模板:
{% if user.is_authenticated and not firstname_of_logged_user == None%}
<h1 class="text-center" id="extraglow">{{firstname_of_logged_user}}! where do
you want to go?</h1></label>
{%else%}
<h1 class="text-center" id="extraglow">where do you want to go?</h1></label>
{%endif%}
Run Code Online (Sandbox Code Playgroud)
但是,似乎没有选择“ not None”部分,因为如果用户登录但未输入名字,则显示如下:
!你想去哪里?
如果我说:
firstname_of_logged_user is not None
Run Code Online (Sandbox Code Playgroud)
它说一个错误:
if表达式末尾未使用的'is'。
似乎很简单,但是却行不通。怎么了?干杯!