我正在做一个可以写评论的板。你知道吗
所有的功能都很好。你知道吗
但我唯一需要的是显示评论作者的个人资料。你知道吗
{{ 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)
您应该在配置文件模型中添加其字段的相关名称
之后你可以用{{comment.whose.profile文件}}你知道吗
相关问题 更多 >
编程相关推荐