i"m working on a project with django something blogging like webapp. I'm using django notifications for my websites notification. i'm recieving notifications if someone comment on my post or like the post. However i can't go to the specific post from the notifications by clicking the notification. my views.py :
@login_required
def like_post(request):
# posts = get_object_or_404(Post, id=request.POST.get('post_id'))
posts = get_object_or_404(post, id=request.POST.get('id'))
# posts.likes.add for the particular posts and the post_id for the post itself its belongs to the post without any pk
is_liked = False
if posts.likes.filter(id=request.user.id).exists():
posts.likes.remove(request.user)
is_liked = False
else:
posts.likes.add(request.user)
is_liked = True
notify.send(request.user, recipient=posts.author, actor=request.user, verb='liked your post.', nf_type='liked_by_one_user')
context = {'posts':posts, 'is_liked': is_liked, 'total_likes': posts.total_likes(),}
if request.is_ajax():
html = render_to_string('blog/like_section.html', context, request=request)
return JsonResponse({'form': html})
From the project's readme, we can see that the Notification model allows you to store extra data in a JSON field. To enable this, you'll first need to add this to your settings file
DJANGO_NOTIFICATIONS_CONFIG = { 'USE_JSONFIELD': True}
After doing so you can store the url of the target object in the field by passing it as a kwarg to the notify.send signal
notify.send(request.user, recipient=posts.author, actor=request.user, verb='liked your post.', nf_type='liked_by_one_user', url=object_url)
You should note however that doing it this way would result in a broken link should you change your url conf so an alternative way of doing it would be to create a view that will return the target objects url which you can call while rendering the notifications.