在Django中获取ID

卢卡斯

我有一个用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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Django / python如何从对象列表中获取ID列表

来自分类Dev

从选择框中获取ID

来自分类Dev

使用python Paypal REST SDK在Django中动态获取付款和付款人ID

来自分类Dev

在SBCL中获取线程ID

来自分类Dev

从表格结果中获取ID

来自分类Dev

在TableViewRow中获取行ID

来自分类Dev

在laravel中获取产品ID

来自分类Dev

如何获取在Django模型中自动生成的主键ID

来自分类Dev

在ModelViewSet中创建后,获取Django记录的ID

来自分类Dev

从Codeigniter中的URL获取ID

来自分类Dev

在mongodb中获取最大_id

来自分类Dev

从模态中的URL获取ID

来自分类Dev

从Django API中获取外键对象而不是ID

来自分类Dev

在C ++中获取线程ID

来自分类Dev

如何从Django ORM中的自下而上的方法中获取ID

来自分类Dev

获取formGroup中的表单ID

来自分类Dev

Django:按外键分组,然后从分组中获取最大ID

来自分类Dev

获取Django中HTML形式的多个按钮之一的ID

来自分类Dev

Django获取许多ID的对象

来自分类Dev

在JavaScript中获取Django网站的会话ID

来自分类Dev

Django管理员:在django中获取登录的用户ID

来自分类Dev

如何从Django模型获取ID

来自分类Dev

如何从Django模型获取ID

来自分类Dev

需要获取id而不是modelchoicefield django中的选定值

来自分类Dev

从列表中获取ID

来自分类Dev

如何在 Django 序列化程序中获取外键 ID 而不是引用模型

来自分类Dev

在 Django 中获取表单集的 id 数量

来自分类Dev

从表单中获取 id

来自分类Dev

从 django 类中的 url 获取 id