From 533301e5207534e9c6c164029e35d127a3040d80 Mon Sep 17 00:00:00 2001 From: Nadege Date: Mon, 9 Dec 2019 18:24:21 +0100 Subject: [PATCH] SSE --- news/settings.py | 6 +++++- posts/templates/posts/notice.html | 6 ++++++ posts/views.py | 2 ++ requirements.txt | 3 +++ templates/base.html | 3 +++ 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/news/settings.py b/news/settings.py index 981a3cf..cbcb581 100644 --- a/news/settings.py +++ b/news/settings.py @@ -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 diff --git a/posts/templates/posts/notice.html b/posts/templates/posts/notice.html index 4f4f6ce..bb2fc92 100644 --- a/posts/templates/posts/notice.html +++ b/posts/templates/posts/notice.html @@ -16,6 +16,12 @@ + {% endblock %} diff --git a/posts/views.py b/posts/views.py index 8673b8e..a63792e 100644 --- a/posts/views.py +++ b/posts/views.py @@ -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) diff --git a/requirements.txt b/requirements.txt index bb94555..45925a9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,6 @@ Django==2.2.6 pytz==2019.3 sqlparse==0.3.0 +channels +django-eventstream +djangorestframework diff --git a/templates/base.html b/templates/base.html index 583c120..1b5732e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -14,6 +14,9 @@ +{% load static %} + + {% block title %}First Django Application{% endblock %}