from django.http import JsonResponse | from django.http import JsonResponse | ||||
from rest_framework.parsers import JSONParser | from rest_framework.parsers import JSONParser | ||||
from django.views.decorators.csrf import csrf_exempt | from django.views.decorators.csrf import csrf_exempt | ||||
from django_eventstream import send_event | |||||
# Create your views here. | # Create your views here. | ||||
if request.method == "POST": | if request.method == "POST": | ||||
form = NoticeForm(request.POST) | form = NoticeForm(request.POST) | ||||
if form.is_valid(): | if form.is_valid(): | ||||
newNotice = Notice(notice_title=form.cleaned_data['title'], | |||||
new_Notice = Notice(notice_title=form.cleaned_data['title'], | |||||
notice_text=form.cleaned_data['text'], | notice_text=form.cleaned_data['text'], | ||||
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() | |||||
#return redirect('index') | |||||
new_Notice.save() | |||||
send_event('notice', 'message', new_Notice.id) | |||||
return redirect('index') | |||||
context = {'form': NoticeForm()} | context = {'form': NoticeForm()} | ||||
return render(request, 'posts/edit.html', context) | return render(request, 'posts/edit.html', context) | ||||
if deleteId != None: | if deleteId != None: | ||||
delNotice = Notice.objects.get(id=deleteId) | delNotice = Notice.objects.get(id=deleteId) | ||||
if delNotice != None: | if delNotice != None: | ||||
send_event('notice', 'message', delNotice.id) | |||||
delNotice.delete() | delNotice.delete() | ||||
return redirect('index') | return redirect('index') | ||||
<!DOCTYPE html> | <!DOCTYPE html> | ||||
{% load static %} | |||||
<html lang="en"> | <html lang="en"> | ||||
<head> | <head> | ||||
<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"> | ||||
<meta charset="UTF-8"> | <meta charset="UTF-8"> | ||||
<title>{% block title %} Platzhalter {% endblock %}</title> | <title>{% block title %} Platzhalter {% endblock %}</title> | ||||
</head> | </head> | ||||
<body> | <body> | ||||
{% block content %} Platzhalter {% endblock %} | {% block content %} Platzhalter {% endblock %} | ||||
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> | <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> |
{% extends 'base.html' %} | |||||
{% block title %} | |||||
Index | |||||
{% endblock %} | |||||
{% extends "base.html" %} | |||||
{% block title %}Index.html{% endblock %} | |||||
{% block content %} | {% block content %} | ||||
<script> | |||||
var es = new ReconnectingEventSource('/events/'); | |||||
es.addEventListener('message', function (e) { | |||||
console.log(e.data); | |||||
location.reload(); | |||||
}, false); | |||||
</script> | |||||
<nav class="navbar navbar-default"> | |||||
<div class="container-fluid"> | |||||
<div class="navbar-header"> | |||||
<a class="navbar-brand" href="https://www.th-nuernberg.de/fakultaeten/efi/">Efi</a> | |||||
<a class="navbar-brand" href="https://www.google.de">Google</a> | |||||
<a class="navbar-brand" href="/posts">Home</a> | |||||
<form method="post"> | |||||
{% for n in notices %} | |||||
<div> | |||||
<p>Titel: {{ n.notice_title }}</p> | |||||
<p>Text: {{ n.notice_text }}</p> | |||||
<p><a href="{% url 'delete' deleteId=n.id %}" class="btn btn-danger" >Löschen</a></p> | |||||
</div> | </div> | ||||
<ul class="nav navbar-nav"> | |||||
<li class="active"><a href="#">Home</a></li> | |||||
<li><a href="/posts">Page 1</a></li> | |||||
<li><a href="">Page 2</a></li> | |||||
<li><a href="h">Page 3</a></li> | |||||
</ul> | |||||
</div> | |||||
</nav> | |||||
<div class="container"> | |||||
<div class="jumbotron"> | |||||
<h1>Jumbotron Test</h1> | |||||
<p>My Jumbotron test</p> | |||||
</div> | |||||
<p>This is some text.</p> | |||||
<p>This is another text.</p> | |||||
</div> | |||||
{% endblock %} | |||||
{% endfor %} | |||||
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p> | |||||
</form> | |||||
{% endblock %} |