django - 在 django 中访问不同用户的信息
问题描述
我知道要访问我们使用的登录用户的数据request.user
。我的目标是列出表格中的所有用户并链接到他们的个人资料页面。如何使链接转到用户的个人资料页面?
我有以下内容:
# app/views.py
def tutors_list(request):
user_list = CustomUser.objects.all()
context = {
'user_list': user_list
}
return render(request, 'tutors_list.html', context)
def show_profile(request, username):
user = CustomUser.objects.get(username = username) ### NOT DISPLAYING RIGHT USER
#user = CustomUser.objects.get(id=id)
context = {
'user': user
}
return render(request, 'show_profile.html', context)
# myproject/urls.py
url_patterns = [
# ...
path('show_profile/', views.show_profile, name='show_profile'),
# ...
我收到一条错误消息,说show_profile
期望再有 1 个参数,username
. 如果我需要为数据库中的特定用户而不是登录用户提取数据,该模型将如何工作?
解决方案
正如您的错误所说 show_profile
,预计还有 1 个参数,username
所以您需要在您的 url 模式中传递用户名:
path('<str:username>/show_profile/', views.show_profile, name='show_profile'),
推荐阅读
- angular - Angular 类型检查的工作原理
- laravel - 在 Vue JS 中检查是否使用 Sentinel 登录
- android - 在 onClick 方法中删除“视图”时如何解决错误?
- python-3.x - 如何从 json arrary Django Restframework 一起保存父表和子表
- mysql - 我需要根据来自另一个数据库 nabe 的列值在内部联接中映射数据库名称,需要在内部联接中使用 concat 函数
- css - 如何使图像幻灯片的叠加层向右显示在 css3 动画中的图像
- google-places-api - 每次用户查看我网站上的页面以显示图像时,我是否需要请求 Google-places-api?
- linux - cpupower 监视器中的 C-state Cx 是什么
- c# - 当我调用 ExecuteScalar 时,',' 附近的语法不正确
- java - 从属性文件中读取反斜杠