enter image description herei'm sending notifications to a user via django notifications.. and i have username regex working on the html so anyone posts with @username it wil post and the html is linkable so click on the @username it will take hie to the username profile page. Now i am using django signals to match the username and print out the username. but when i use notify to send the notification. i cant't find the recipient (the user who will get the notification). its giving me a error ValueError at /post/new/
Cannot assign "<_sre.SRE_Match object; span=(0, 4), match='@boy'>": "Notification.recipient" must be a "User" instance.
my models.py:
class post(models.Model):
parent = models.ForeignKey("self", on_delete=models.CASCADE, blank=True, null=True)
title = models.CharField(max_length=100)
image = models.ImageField(upload_to='post_pics', null=True, blank=True)
video = models.FileField(upload_to='post_videos', null=True, blank=True)
content = models.TextField()
likes = models.ManyToManyField(User, related_name='likes', blank=True)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
objects = postManager()
def __str__(self):
return self.title
class Meta:
ordering = ['-date_posted', 'title']
def get_absolute_url(self):
return reverse ('blog-home')
def total_likes(self):
return self.likes.count()
def post_save_receiver(sender, instance, created, *args,**kwargs):
if created and not instance.parent:
user_regex = r'@(?P<username>[\w.@+-]+)'
m = re.search(user_regex, instance.content)
if m:
username = m.group("username")
notify.send(instance.author, recipient=m, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')
post_save.connect(post_save_receiver, sender=post)
The recipient
can not be a Match
object:
notify.send(instance.author, recipient=m, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')
You can try to obtain the User
object with:
if m:
try:
recipient = User.objects.get(username=m.group('username'))
except (User.DoesNotExist, User.MultipleObjectsReturned):
pass
else:
notify.send(instance.author, recipient=recipient, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')