robert
robert

Reputation: 31

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()

I keep getting this error its in the view its a django view it says that the problem is in the u = p.user I get this error, NameError at /friendship/profile/ global name 'user' is not defined

Upvotes: 0

Views: 334

Answers (2)

Robin Zigmond
Robin Zigmond

Reputation: 18249

The error appears to be in this line:

p = Profile.objects.filter(user=user).first()

Specifically, the user you are checking against (to the right of the = sign) is a variable that doesn't appear to be defined anywhere.

Without some more context as to what you're trying to do, it's hard to say how to fix it. I would say though that either you mean to check against the user making the request - in which case use request.user - or you have some parameters in your URL which you should be getting this value from.

Upvotes: 0

Amadan
Amadan

Reputation: 198324

You can't get that error at the line you quote (u = p.user). But you can, and should, get it on the previous line. Replace .filter(user=user) with .filter(user=request.user).

Upvotes: 2

Related Questions