我正在使用Django创建一个博客网站。我想在一个帖子中获得评论的数量(我有一个可以调用的完整的帖子字典)作为int并在以下def中使用它:
def top_three(request)
first = -1
second = -1
third = -1
com = Comment.objects.filter(approved_comment)
posts = Post.objects.filter(comments__in=com)
for post in posts:
if len(posts) >= 3:
postvar = len(com)
if postvar > first:
first = postvar
one = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
elif postvar > second:
second = postvar
two = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
elif postvar > third:
third = postvar
three = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
else:
return render(request, 'blog/home.html')
return render(request, 'blog/home.html', {
'one': one,
'two': two,
'three': three,
})
假设我已经导入了所有需要导入的内容
评论模型与Post模型直接相关,没有相关帖子的人不能发表评论。我的模特是:
class Post(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
url = models.TextField()
title = models.CharField(max_length=200)
discription = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
def approved_comments(self):
return self.comments.filter(approved_comment=True)
和
class Comment(models.Model):
post = models.ForeignKey('blog.Post', related_name='comments')
author = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
def approve(self):
self.approved_comment = True
self.save()
def __str__(self):
return self.text
你如何建议我在博客/ home.html中拨打一,二和三?
答案 0 :(得分:1)
如果您想要批准评论的每个帖子,请执行
public function update(Request $request, int $employee_id) {
$this->validate ( $request, [
'first_name' => "required|max:220|regex:/[a-z]/",
'middle_name' => "max:120",
'last_name' => "required|max:220|regex:/[a-z]/",
'email' => "required|unique:employees,email,".$employee_id.',employee_id|regex:/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}/',
'home_phone' => "unique:employees,home_phone,$employee_id,employee_id|numeric",
'mobile' => "required|unique:employees,mobile,$employee_id,employee_id|numeric",
'job_id' => 'required',
'department_id' => 'required',
'group_id' => 'required',
'node' => 'required',
'branch' => 'required',
'username' => "required|unique:employees,username,".$employee_id.',employee_id|regex:/[A-Za-z0-9][.][A-Za-z0-9]/',
'exchange_username' => "required|unique:employees,exchange_username,".$employee_id.',employee_id|regex:/[A-Za-z0-9][.][A-Za-z0-9]/',
'extension' => "required|unique:employees,mobile,$employee_id,employee_id|numeric|regex:/[0-9]{4}/",
] );
Employee::where ('employee_id', $employee_id )->update ( $request->only ( [
'first_name',
'middle_name',
'last_name',
'email',
'address',
'home_phone',
'mobile',
'job_id',
'department_id',
'group_id',
'branch',
'node',
'name',
'username',
'type',
'exchange_username',
'toggle_ivr_access',
'extension',
'attributed_team',
'cable_team_id',
'disable',
] ) );
Session::flash ( 'message', 'The Employee is Successfully Updated.' );
return redirect ()->route ( 'employees.index' );
}