'django.contrib.staticfiles', | 'django.contrib.staticfiles', | ||||
'posts', | 'posts', | ||||
'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 = 'news1.urls' | ROOT_URLCONF = 'news1.urls' | ||||
] | ] | ||||
WSGI_APPLICATION = 'news1.wsgi.application' | WSGI_APPLICATION = 'news1.wsgi.application' | ||||
ASGI_APPLICATION = 'news1.routing.application' | |||||
# Database | # Database |
<div class="container"> | <div class="container"> | ||||
{% for notice in notices %} | {% for notice in notices %} | ||||
<h3> {{ notice.notice_title }}</h3> | <h3> {{ notice.notice_title }}</h3> | ||||
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p> | <p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p> | ||||
<p><a href="{% url 'logout' %}" class="btn btn-warning">Abmelden</a></p> | <p><a href="{% url 'logout' %}" class="btn btn-warning">Abmelden</a></p> | ||||
{% endblock %} | |||||
</div> | |||||
<script> | |||||
var es = new ReconnectingEventSource('/events/'); | |||||
es.addEventListener('message',function(e) { | |||||
console.log(e.data); location.reload();false | |||||
}) | |||||
</script> | |||||
{% endblock %} |
from django.contrib.auth.decorators import login_required | from django.contrib.auth.decorators import login_required | ||||
from django.shortcuts import render, redirect | from django.shortcuts import render, redirect | ||||
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) | ||||
delNotice= Notice.objects.get(id=deleteId) | delNotice= Notice.objects.get(id=deleteId) | ||||
if delNotice!= None: | if delNotice!= None: | ||||
delNotice.delete() | delNotice.delete() | ||||
send_event('notice', 'message', deleteId) | |||||
return redirect('index') | return redirect('index') | ||||
@csrf_exempt | @csrf_exempt | ||||
def notice_list(request): | def notice_list(request): |
<!DOCTYPE html> | <!DOCTYPE html> | ||||
<html lang="fr"> | <html lang="fr"> | ||||
<head> | <head> | ||||
{% load static %} | |||||
<script src="{% static 'django_eventstream/eventsource.min.js' %}"></script> | |||||
<script src="{% static 'django_eventstream/reconnecting-eventsource.js' %}"></script> | |||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> | <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> | ||||
</section> | </section> | ||||
<footer>© posts news1</footer> | <footer>© posts news1</footer> | ||||
</body> | </body> | ||||
</html> | |||||
</html> |