如何在 Django 中获取请求用户的数据?

How to fetch data of the request user in Django?

我正在创建个人资料页面,需要获取请求用户的 id,然后我可以循环它的信息。

我正在使用这个视图:

def profile(request):  
    if request.user.is_authenticated:
        user = request.user
        us = User.objects.get(username=user)
        context = { 'us': us }
    return render(request, context, "frontend/profile.html")

路径:

path('perfil/', views.profile, name="profile")

但是当我尝试访问该页面时出现以下错误:

join() argument must be str, bytes, or os.PathLike object, not 'dict'

该用户已在您的模板中可用:{{user.username}} 或您想要的任何内容。

对于您的代码,渲染没有好的参数:

return render(request,"frontend/profile.html", context)