I'm a beginner's level Django developer and I'm making a hospital system. In the hospital system, I want to add a notification system whenever any patient uploads a report against the doctor id I want to inform the doctor to the doctor notifications template that the report of patient(with name) has been submitted.
I was trying it to work for 2 days and used signals to create a message but I'm unable to show it on the doctor's side. Please anyone can tell me how can I make it easier or Should I use something else instead of signals.
The Previous Code that I tried.
from django.shortcuts import render, HttpResponse,request
from hospital.models import Patient ,Doctor,Report
from django.db.models.signals import post_save,pre_save
def save_report(sender,instance,**kwargs):
instance = Patient.objects.get(id=1)
dr_id= instance.assignedDoctorId
patient=models.Patient.objects.get(user_id=request.user.id)
doctor=models.Doctor.objects.get(id=dr_id)
notifications=print("New report has been submitted")
mydict={
'doctor':doctor,
'patient':patient,
'notifications':notifications
}
return render(request,'temp/doctor_Notifications.html',context=mydict)
post_save.connect(save_report,sender=Report)
You should create model of Notification like:
class Notification(models.Model):
person = models.ForeignKey(Mymodel, on_delete=models.CASCADE)
is_read = models.BooleanField(default=False)
message = models.TextField(max_length=100)
In your views simply create Notification whenever patient sends report:
def save_report(sender,instance,**kwargs):
instance = Patient.objects.get(id=1)
dr_id= instance.assignedDoctorId
patient=models.Patient.objects.get(user_id=request.user.id)
doctor=models.Doctor.objects.get(id=dr_id)
Notification.objects.create(person=doctor, message=f'{instance} sent you a report!')
mydict={
'doctor':doctor,
'patient':patient,
}
return render(request,'temp/doctor_Notifications.html',context=mydict)
Create view for displaying notifications, with switching to read when launched:
class NotificationListView(ListView):
model = Notification
template_name = 'users/notifications.html'
def get_queryset(self):
notifications = Notification.objects.filter(person=self.request.user).all()
notifications.update(is_read=True)
return notifications
template with notifications:
{% block content %}
{% for notification in object_list %}
<h6>{{ notification.message }}</h6>
{% endfor %}
{% endblock %}
If you have navbar in your site you probably want to display number of unread notifications. You have to filter unread notifications first: In your app folder create new one called "templatetags" and inside create: init .py and notifilter.py for example.
-yourApp
-templatetags
-__init__.py
-notifilter.py
notifilter.py
from django import template
register = template.Library()
@register.filter
def notifilter(args):
filtered = []
for i in args:
if i.is_read == False:
filtered.append(i)
return filtered
load filter in template when you have navbar and add line like below:
{% load notifilter %}
<a class="nav-link" href="{% url 'notifications' %}">notifications({{ request.user.profile.notification_set.all|notifilter|length }})</a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.