获取temp中另一个孩子的数据

2024-04-20 02:34:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个可以写评论的板。你知道吗

所有的功能都很好。你知道吗

但我唯一需要的是显示评论作者的个人资料。你知道吗

{{ board.title }}
{{ board.text }}

{% for comment is context.commentmodel_set.all %}
    {{ comment.comment }}
    {{ comment.whose.profile??? }} <- ????
{% endfor %}

///////////编辑[添加模型]/////////

# Appname : board / model
class Board(models.Model):
    whose = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    title = models.CharField()
    text = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

class Comment(models.Model):
    post = models.ForeignKey(Board, on_delete=models.CASCADE)
    whose = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    comment = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)


# Appname : accounts / model
class Profile(models.Model):
    whose = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    i_need = models.CharField()
    address = models.CharField()
    tel = models.CharField()

/////编辑秒[视图.注释]////

@login_required
def comment(request, pk):
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.post = Board.objects.get(pk=pk)
            post.whose = request.user
            post.save()
            return redirect('board_detail', pk)
        else:
            print('TurminalCheck : Invalid!!')
    else:
        form = CommentForm()

    context = {
        'form': form,
    }
    return render(request, 'boardapp/board.html', context)

供参考的图片附在下面。 enter image description here


Tags: formboardautoonmodelsrequestcommentdelete
1条回答
网友
1楼 · 发布于 2024-04-20 02:34:03

您应该在配置文件模型中添加其字段的相关名称

class Profile(models.Model):
    whose = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="profile")
    i_need = models.CharField()
    address = models.CharField()
    tel = models.CharField()

之后你可以用{{comment.whose.profile文件}}你知道吗

相关问题 更多 >