Django 将id传递给用户以查看函数

Django 将id传递给用户以查看函数,django,authentication,url,view,Django,Authentication,Url,View,我尝试通过模板传递id以查看,但我没有 在/accounts/profile/1处出现多值错误 路径('accounts/profile/',views.profile,name=“profile”), def配置文件(请求,id): user=user.objects.get(id=int(request.POST[id])) 返回用户 由于id作为URL参数传递,因此您可以使用以下选项进行筛选: from django.shortcuts import get_object_or_404

我尝试通过模板传递id以查看,但我没有

在/accounts/profile/1处出现多值错误


路径('accounts/profile/',views.profile,name=“profile”),
def配置文件(请求,id):
user=user.objects.get(id=int(request.POST[id]))
返回用户

由于id作为URL参数传递,因此您可以使用以下选项进行筛选:

from django.shortcuts import get_object_or_404

def profile(request, id):
    user = get_object_or_404(User, pk=id)
    # …
从django.shortcuts导入get\u object\u或\u 404
def配置文件(请求,id):
用户=获取对象或404(用户,pk=id)
#…

此外,您不能
返回用户
,因为这是一个
用户
对象,而不是
HttpResponse
,因此您需要返回例如呈现模板的结果等。

堆栈跟踪说什么?是的,这解决了我的问题,谢谢:)。如果我没有在url中传递参数并且wan没有从请求中获取id,我该怎么做?@darek_82:那么它就是
pk=request.POST['id']
(或者
request.get
,具体取决于方法)。
from django.shortcuts import get_object_or_404

def profile(request, id):
    user = get_object_or_404(User, pk=id)
    # …