django – Why can't request.user not see its own message inbox?


So I'm trying to interrogate and display messages between two users in an inbox. I'm having a problem where no message appears for request.user's mailbox. It appears blank when there are messages. However, when I go to an inbox for someone other than my request.user sent a message while he was still connected to request.user, I can see the messages from both parties and displayed correctly. I don't understand why this is happening. Hell, request.user.username is passed through the template and the username of my user actually prints before and after the for loop. But this is not the case if I do it if msg.sender_id==request.user Yes, django.template.context_processors.request is already in my context processor. I tried to do if msg.receiver_id == request.user but it still doesn't work. What am I doing wrong here? And I'm using admin.py to make messages right now

views.py/messages

def messages(request, profile_id):
    messages = InstantMessage.objects.filter(Q(sender_id=request.user, receiver_id=profile_id,) 
            | Q(sender_id=profile_id, receiver_id=request.user,) ).
        values('sender_id','receiver_id', 'message', 'date', ).
        order_by('date',)


    return render(request, 'dating_app/messages.html', {'messages': messages,})

messages.html

{% for msg in messages %}


    {% if msg.receiver_id == user.id %}
        
  • {{ msg.message }}
    {{ msg.date }}
    {{ request.user.username }}
  • {% elif msg.sender_id == user.id %}
  • {{ msg.message }}
    {{ msg.date }}
  • {% endif %} {% empty %} {%endfor %}

    urls.py/messages

    path('messages//', views.messages, name='messages')
    

    models.py

    class ProfileManager(BaseUserManager):
    
    
    
        def create_user(self, username, email,description,photo, password=None):
            if not email:
                raise ValueError("You must creat an email")
            if not username:
                raise ValueError("You must create a username!")
            if not description:
                raise ValueError("You must write a description")
            if not photo:
                raise ValueError("You must upload a photo")
    
            user = self.model(
                    email=self.normalize_email(email),
                    username = username, 
                    description= description,
                    photo= photo,
    
                )
    
            user.set_password(password)
            user.save(using=self._db)
            return user 
    
    
        def create_superuser(self, username, email,description,photo, password):
            user = self.create_user(
                    email=self.normalize_email(email),
                    password=password,
                    username=username,
                    description=description,
                    photo=photo,
    
                )
    
            user.is_admin=True
            user.is_staff=True
            user.is_superuser=True
            user.save(using=self._db)
            return user
    
    
    
    
    class Profile(AbstractBaseUser):
    
        class Meta:
            swappable = 'AUTH_USER_MODEL'
    
    
        email                       = models.EmailField(verbose_name="email")
        username                    = models.CharField(max_length=30, unique=True)
        date_joined                 = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
        last_login                  = models.DateTimeField(verbose_name='last login', auto_now=True)
        is_admin                    = models.BooleanField(default=False)
        is_active                   = models.BooleanField(default=True)
        is_staff                    = models.BooleanField(default=False)
        is_superuser                = models.BooleanField(default=False)
        #what I added
        description                 = models.TextField()
        photo                       = models.ImageField(upload_to='profile_photo',blank=False, height_field=None, width_field=None, max_length=100)
        matches                     = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='+', blank=True)
    
    
    
        USERNAME_FIELD = 'username'
        REQUIRED_FIELDS = ('description','photo','email')
    
    
        objects = ProfileManager()
    
    
        def __str__(self):
            return self.username
    
    
        def has_perm(self, perm, obj=None):
            return self.is_admin
    
    
        def has_module_perms(self,app_label):
            return True
    
    
    
    class UserVote(models.Model):
    
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        voter = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='given_vote', on_delete=models.CASCADE)
        vote = models.BooleanField(default=False)
    
        class Meta:
            unique_together = (('user', 'voter'))
    
    
    
    class InstantMessage(models.Model):
    
        sender = models.ForeignKey(settings.AUTH_USER_MODEL, related_name= 'senderr',on_delete=models.CASCADE )
        receiver = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        message = models.TextField()
        date = models.DateTimeField(auto_now_add=True)
    
    
        def __unicode__(self):
            return self.messaage