Reputation: 71
so i currently have my likes app which deals with friend requests, and it works fine however my notification dont seem to be working. Whenever some likes someone else regardless of weather they are liked by that user or not it only sends the second of the two notify.send. I presume its an issue with the line "user = get_object_or_404(User, username=user.username)", however i dont know how to get round it. Here is my code:
def like_user(request, id):
pending_like = get_object_or_404(User, id=id)
user_like, created = UserLike.objects.get_or_create(user=request.user)
user = get_object_or_404(User, username=user.username)
liked_user, like_user_created = UserLike.objects.get_or_create(user=user)
if pending_like in user_like.liked_users.all():
user_like.liked_users.remove(pending_like)
elif request.user in liked_user.liked_users.all():
user_like.liked_users.add(pending_like)
notify.send(request.user,
#action=request.user.profile,
target=request.user.profile,
recipient=pending_like,
verb='sent you a friend request view'),
else:
user_like.liked_users.add(pending_like)
notify.send(request.user,
#action=request.user.profile,
target=request.user.profile,
recipient=pending_like,
verb='accepted your friend request view')
return redirect("profile", username=pending_like.username)
Here is an example of where the line " if request.user in liked_user.liked_users.all():" works fine i presume because the line " user = get_object_or_404(User, username=username)" has username in it.
@login_required
def profile_view(request, username):
user = get_object_or_404(User, username=username)
liked_user, like_user_created = UserLike.objects.get_or_create(user=user)
do_they_like = False
if request.user in liked_user.liked_users.all():
do_they_like = True
However in my first bit of code I'm trying to use user.username instead of username=username but i get the error "local variable 'user' referenced before assignment". What is the best way round this? am i do it completely wrong? should i try and pass in username, because when i do i get the error "like_user() takes exactly 3 arguments (2 given)". Sorry quite new to django, any help would be massively appreciated!
Here is my likes app model incase it helps:
class UserLike(models.Model):
user = models.OneToOneField(User, related_name='liker')
liked_users = models.ManyToManyField(User, related_name='liked_users', blank=True)
objects = UserLikeManager()
def __unicode__(self):
return self.user.username
def get_mutual_like(self, user_b):
i_like = False
you_like = False
if user_b in self.liked_users.all():
i_like = True
liked_user, created = UserLike.objects.get_or_create(user=user_b)
if self.user in liked_user.liked_users.all():
you_like = True
if you_like and i_like:
return True
else:
return False
really sorry for the long post, but im very stuck as most of this was written by some more advanced than me and im left with the issue of fixing it, any help would be massively appreciated!
thanks
Upvotes: 0
Views: 1816
Reputation: 684
You are correct, the issue is with the line:
user = get_object_or_404(User, username=user.username)
You're trying to get the attribute 'username' of the 'user' object, but 'user' is not defined within the scope of your function yet. In other words, at that point 'user' doesn't exist yet.
I don't have nearly enough info to start helping you, but if it was me debugging that, I'd consider the following:
You might not need that problematic line at all. It seems like request.user holds the reference to a user (The one who's liking stuff??), and pending_like seems to contain a reference to the recipient of the like. That's probably enough users to establish a 'like' relationship
Maybe do a print of the attributes of pending_like print(pending_like.__dict__)
and request.user print(request.user.__dict__)
to see if they contain all the info you need?
Upvotes: 3