[英]Django project global name 'user' is not defined
NameError at /friendship/profile/
global name 'user' is not defined
\views.py in profile_view, line 51
def profile_view(request):
p = Profile.objects.filter(user=user).first()
u = p.user
sent_friend_requests = FriendRequest.objects.filter(from_user=p.user)
rec_friend_requests = FriendRequest.objects.filter(to_user=p.user)
friends = p.friends.all()
我一直在查看它的django視圖中收到此錯誤,它表示問題出在u = p.user中。
您不能在引用行( u = p.user
)處收到該錯誤。 但是您可以並且應該在上一行中找到它。 用.filter(user=request.user)
替換.filter(user=user)
.filter(user=request.user)
。
錯誤似乎在此行中:
p = Profile.objects.filter(user=user).first()
具體來說,您要檢查的user
(在=
號右邊)是一個似乎未在任何地方定義的變量。
如果沒有關於您要執行的操作的更多上下文,那么很難說出解決方法。 我要說的是,要么意味着要檢查發出請求的用戶(在這種情況下,請使用request.user
,要么您的URL中有一些參數,您應該從中獲取該值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.