Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取模板中另一个子级的数据_Python_Django_Django Templates - Fatal编程技术网

Python 获取模板中另一个子级的数据

Python 获取模板中另一个子级的数据,python,django,django-templates,Python,Django,Django Templates,我正在制作一个可以写评论的板 所有功能都运行良好 但我唯一需要的是显示评论作者的个人资料 {{ board.title }} {{ board.text }} {% for comment is context.commentmodel_set.all %} {{ comment.comment }} {{ comment.whose.profile??? }} <- ???? {% endfor %} /////编辑第二个[视图.注释]////// @login_r

我正在制作一个可以写评论的板

所有功能都运行良好

但我唯一需要的是显示评论作者的个人资料

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

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

/////编辑第二个[视图.注释]//////

@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)
供参考的图片附在下面。

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

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.which.profile}}

调用profile之后,您可以显示注释、用户和概要文件模型吗?它不就是
{{comment.which.profile.i_need}
?但是你应该展示实际的模型。@never我刚刚更新过。我使用Django用户系统。所以我没有用户模型。就这些。看起来丹尼尔·罗斯曼建议的代码应该可以用了。你试过了吗?我试过了。但它不起作用…非常感谢:)非常简单和干净!
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()