我有一个用Django创建的网站,人们可以在其中发布问题并回答它们-就像Stack Overflow的副本一样。当用户访问http:// localhost /?post = Example时,它将过滤数据库中名称为example的帖子。稍后,我将这样做,只有一个具有相同名称的帖子。
目前,它仅使用{{post.author}}在HTML文档中呈现帖子/问题。
posts = Post.objects.filter(title=post)
context = {'posts':posts}#, "answers":answers}
return render(request, 'store/post.html', context)
在堆栈溢出时,可以回答一个问题。可以有多个。因此,在我的代码中,我是通过在名为Answer的模型中创建外键来实现的,该模型将答案链接到帖子。
在HTML文档中,我做了一个for循环-就像我对发布/问题所做的那样,遍历了所有答案并在问题下显示了它们。
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank = True)
name = models.CharField(max_length=200, null=True)
email = models.EmailField(max_length=200, null=True)
about = models.CharField(max_length=100, null=True, help_text="Use this field for notes about the customer.")
image = models.ImageField(null=True, blank=True)
@property
def imageURL(self):
try:
url = self.image.url
except:
url = 'placeholder.png'
return url
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=200, null=True)
context = models.TextField(max_length=1702, blank=True, validators=[MaxLengthValidator(1702)])
date = models.DateField(("Date"), default=date.today)
author = models.ForeignKey(
Customer,
on_delete=models.CASCADE,
null=True
)
def __str__(self):
return self.title
class Answer(models.Model):
title = models.CharField(max_length=200, null=True)
context = models.TextField(max_length=1702, blank=True, validators=[MaxLengthValidator(1702)])
date = models.DateField(("Date"), default=date.today)
author = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True)
post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.title
因此,我认为我必须按ID进行操作,因为当我查看SQL浏览器(SQLCIPHER)时,它仅显示一个整数,但我不确定。
这是我用于过滤帖子和答案的代码。我只是不确定如何获取变量postID以及这是否是正确的方法。
posts = Post.objects.filter(title=post)
answers = Answer.objects.filter(post=postID)
context = {'posts':posts, "answers":answers}
return render(request, 'store/post.html', context)
这是post.html网站:
{% for post in posts %}
<div class="blogpost">
<a class="post-title">{{ post.title }}</a>
<a class="date-author">{{ post.date }} - {{ post.author }}</a>
<div class="post-author-wrapper">
<img class="thumbnail" src="{{ post.author.imageURL }}">
</div>
<div class="blogpost-content">{{ post.context | linebreaks }}</div>
</div>
{% endfor %}
{% for answer in answers %}
<div class="blogpost">
<a class="post-title">{{ answer.title }}</a>
<a class="date-author">{{ answer.date }} - {{ answer.author }}</a>
<div class="post-author-wrapper">
<img class="thumbnail" src="{{post.author.imageURL}}">
</div>
<div class="blogpost-content">
Here is a quick tutorial on how to do it: https://example.com
</div>
</div>
{% endfor %}
如果只有一个帖子,则过滤没有意义,您应该使用get_object_or_404(…)
[Django-doc]:
from django.shortcuts import get_object_or_404
# …
post = get_object_or_404(Post, title=post)
然后post
是单个 Post
对象,而不是对象的集合Post
。然后,您可以利用answer_set
来获取相关答案:
answers = post.answer_set.all()
由于post
是单篇文章,因此您将忽略那些荒谬的内容。{% for post in posts %}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句