@@ -38,7 +38,9 @@ INSTALLED_APPS = [ | |||
'django.contrib.messages', | |||
'django.contrib.staticfiles', | |||
'posts.apps.PostsConfig', | |||
'rest_framework' | |||
'rest_framework', | |||
'channels', | |||
'django_eventstream' | |||
] | |||
MIDDLEWARE = [ | |||
@@ -49,6 +51,7 @@ MIDDLEWARE = [ | |||
'django.contrib.auth.middleware.AuthenticationMiddleware', | |||
'django.contrib.messages.middleware.MessageMiddleware', | |||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | |||
'django_grip.GripMiddleware' | |||
] | |||
ROOT_URLCONF = 'news.urls' | |||
@@ -71,6 +74,7 @@ TEMPLATES = [ | |||
] | |||
WSGI_APPLICATION = 'news.wsgi.application' | |||
ASGI_APPLICATION = 'posts.routing.application' | |||
# Database |
@@ -16,6 +16,12 @@ | |||
</div> | |||
</div> | |||
<script> | |||
var es = new ReconnectingEventSource('/events/'); | |||
es.addEventListener('message', function (e) { | |||
console.log(e.data);location.reload();false | |||
}) | |||
</script> | |||
{% endblock %} | |||
@@ -4,6 +4,7 @@ from django.http import HttpResponse, JsonResponse | |||
from django.shortcuts import render, redirect | |||
from django.utils import timezone | |||
from django.views.decorators.csrf import csrf_exempt | |||
from django_eventstream import send_event | |||
from rest_framework.parsers import JSONParser | |||
from posts.forms import NoticeForm | |||
@@ -36,6 +37,7 @@ def new(request): | |||
pub_start=form.cleaned_data['start'], | |||
pub_end=form.cleaned_data['end']) | |||
newNotice.save() | |||
send_event('notice', 'message', newNotice.id) | |||
return redirect('index') | |||
context = {'form': NoticeForm()} | |||
return render(request, 'posts/edit.html', context) |
@@ -1,3 +1,6 @@ | |||
Django==2.2.6 | |||
pytz==2019.3 | |||
sqlparse==0.3.0 | |||
channels | |||
django-eventstream | |||
djangorestframework |
@@ -14,6 +14,9 @@ | |||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> | |||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> | |||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> | |||
{% load static %} | |||
<script src="{% static 'django_eventstream/eventsource.min.js' %}"></script> | |||
<script src="{% static 'django_eventstream/reconnecting-eventsource.js' %}"></script> | |||
<title>{% block title %}First Django Application{% endblock %}</title> | |||
</head> | |||
<body> |