gpt4 book ai didi

python - 使用 django 显示特定帖子

转载 作者:太空宇宙 更新时间:2023-11-04 02:43:58 25 4
gpt4 key购买 nike

我在 django 中有一个帖子应用程序,不同的员工可以在其中创建帖子。我现在希望能够在他们登录时显示每个发帖人的帖子,而不包括其他帖子。

模型

class Post(models.Model):
"""docstring for Post."""
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) #blank=True, null=True)#default=1
title = models.CharField(max_length = 120)
slug = models.SlugField(unique= True)
draft = models.BooleanField(default = False)
publish = models.DateField(auto_now=False, auto_now_add=False)
content = models.TextField()
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

#objects = PostManager()

def __str__(self):
return self.title

查看

def index(request):
results = Post.objects.all().filter(draft=False)#.filter(publish__lte=timezone.now())
que = request.GET.get("q")
if que:
results =results.filter(
Q(title__icontains=que)|
Q(content__icontains=que)).distinct()
paginator = Paginator(results, 8) # Show 25 contacts per page
pages ="page"
page = request.GET.get('page')
try:
query = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
query = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
query = paginator.page(paginator.num_pages)
context = {
"objects": query,
"pages": pages
}
template = 'index.html'
return render(request,template,context)

其他代码将根据要求上传。谢谢

最佳答案

用户只会看到自己的帖子

results = Post.objects.all().filter(draft=False).filter(publish__lte=timezone.now())
results = results.filter(user=request.user)

关于python - 使用 django 显示特定帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45679455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com