Install Django Notifications App
We will use this app https://github.com/django-notifications/django-notifications – Connect to preview
They have good documentation about how to install and use this app, just read README file.
1.Run command in our terminal:
pip install django-notifications-hq
2.Add to main settings.py:
INSTALLED_APPS = [
....
'django.contrib.auth',
'notifications',
....
]
Important that ‘notifications’ must be after ‘django.contrib.auth’. Also add this string:
DJANGO_NOTIFICATIONS_CONFIG = { 'USE_JSONFIELD': True}
3.To our main urls.py add this code:
from django.conf.urls import url
import notifications.urls
urlpatterns = [
...
url('^inbox/notifications/', include(notifications.urls, namespace='notifications')),
...
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
4.Run migtation command in our terminal:
python manage.py migrate notifications
Now u can use Notifications App.
Customizations for our project
Note: If you want to test with these CSS classes, you can get the open-source CSS file at agilecss.com
1.Add to _header.html file following code inside nav menu
2.Add to the end of _footer.html file this code
Close
3.Add js files in base.html file after _footer.html including
4.Save the following file to our projects js folder and run “python manage.py collectstatic“ command in our terminal
Fire a notification
You can add a notification in view.py in any method
#Make notification to admin
recipient = User.objects.get(id=1)
notify.send(recipient, recipient=recipient, verb='New Contact us request')
This example from contact us page
from .models import Contact_us
def index(request):
if request.method == ‘POST’:
name = request.POST[‘name’]
email = request.POST[’email’]
phone = request.POST[‘phone’]
subject = request.POST[‘subject’]
department = request.POST[‘department’]
message = request.POST[‘message’]
contact_request = Contact_us(name=name, email=email, phone=phone, subject=subject, department=department, message=message)
#Make notification to admin
recipient = User.objects.get(id=1)
notify.send(recipient, recipient=recipient, verb=’New contact us request’)
contact_request.save()
if contact_request.id:
#by defaul using bg-success class
return JsonResponse({‘msg’:’Contact message send’}, safe=False)
else:
return JsonResponse({‘msg’:’Contact message not send’,’class’:’bg-danger’}, safe=False)
return render(request, ‘contact_us/index.html’)