Browse Source

SSE

master
Nadege 5 years ago
parent
commit
533301e520
5 changed files with 19 additions and 1 deletions
  1. 5
    1
      news/settings.py
  2. 6
    0
      posts/templates/posts/notice.html
  3. 2
    0
      posts/views.py
  4. 3
    0
      requirements.txt
  5. 3
    0
      templates/base.html

+ 5
- 1
news/settings.py View File

'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'posts.apps.PostsConfig', 'posts.apps.PostsConfig',
'rest_framework'
'rest_framework',
'channels',
'django_eventstream'
] ]


MIDDLEWARE = [ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_grip.GripMiddleware'
] ]


ROOT_URLCONF = 'news.urls' ROOT_URLCONF = 'news.urls'
] ]


WSGI_APPLICATION = 'news.wsgi.application' WSGI_APPLICATION = 'news.wsgi.application'
ASGI_APPLICATION = 'posts.routing.application'




# Database # Database

+ 6
- 0
posts/templates/posts/notice.html View File

</div> </div>
</div> </div>


<script>
var es = new ReconnectingEventSource('/events/');
es.addEventListener('message', function (e) {
console.log(e.data);location.reload();false
})
</script>


{% endblock %} {% endblock %}



+ 2
- 0
posts/views.py View File

from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.utils import timezone from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django_eventstream import send_event
from rest_framework.parsers import JSONParser from rest_framework.parsers import JSONParser


from posts.forms import NoticeForm from posts.forms import NoticeForm
pub_start=form.cleaned_data['start'], pub_start=form.cleaned_data['start'],
pub_end=form.cleaned_data['end']) pub_end=form.cleaned_data['end'])
newNotice.save() newNotice.save()
send_event('notice', 'message', newNotice.id)
return redirect('index') return redirect('index')
context = {'form': NoticeForm()} context = {'form': NoticeForm()}
return render(request, 'posts/edit.html', context) return render(request, 'posts/edit.html', context)

+ 3
- 0
requirements.txt View File

Django==2.2.6 Django==2.2.6
pytz==2019.3 pytz==2019.3
sqlparse==0.3.0 sqlparse==0.3.0
channels
django-eventstream
djangorestframework

+ 3
- 0
templates/base.html View File

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <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://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> <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> <title>{% block title %}First Django Application{% endblock %}</title>
</head> </head>
<body> <body>

Loading…
Cancel
Save