'django.contrib.sessions', | 'django.contrib.sessions', | ||||
'django.contrib.messages', | 'django.contrib.messages', | ||||
'django.contrib.staticfiles', | 'django.contrib.staticfiles', | ||||
'posts.apps.PostsConfig' | |||||
'posts.apps.PostsConfig', | |||||
'rest_framework' | |||||
] | ] | ||||
MIDDLEWARE = [ | MIDDLEWARE = [ |
urlpatterns = [ | urlpatterns = [ | ||||
path('posts/', include('posts.urls')), | path('posts/', include('posts.urls')), | ||||
path('admin/', admin.site.urls), | path('admin/', admin.site.urls), | ||||
path('accounts/', include('django.contrib.auth.urls')) | |||||
] | ] |
from rest_framework import serializers | |||||
from posts.models import Notice | |||||
class NoticeSerializer(serializers.ModelSerializer): | |||||
class Meta: | |||||
model = Notice | |||||
fields = ('id', 'notice_title', 'notice_text', 'pub_start', 'pub_end') | |||||
{% extends "base.html" %} | {% extends "base.html" %} | ||||
{% block content %} | {% block content %} | ||||
<h1>Neue Nachricht</h1> | |||||
<form method="POST"> | |||||
{% csrf_token %} | |||||
{{ form.as_p }} | |||||
<button type="submit" class="save btn btn-default">Speichern</button> | |||||
</form> | |||||
<h1>Neue Nachricht</h1> | |||||
<form method="POST"> | |||||
{% csrf_token %} | |||||
{{ form.as_p }} | |||||
<button type="submit" class="save btn btn-default">Speichern</button> | |||||
</form> | |||||
{% if user.is_authenticated %} | |||||
<p><a href="{% url 'logout' %}?next=/posts/home" | |||||
class="btn btn-warning">Abmelden</a></p> | |||||
{% endif %} | |||||
{% endblock %} | {% endblock %} |
<div class="card w-100"> | <div class="card w-100"> | ||||
<div class="card-body"> | |||||
<div class="card-body jumbotron" style="background: ghostwhite"> | |||||
{% for notice in allnotice %} | {% for notice in allnotice %} | ||||
<h5 class="card-title">{{ notice.notice_title }}</h5> | <h5 class="card-title">{{ notice.notice_title }}</h5> | ||||
<p class="card-text">{{ notice.notice_text |linebreaks }}</p> | <p class="card-text">{{ notice.notice_text |linebreaks }}</p> | ||||
</div> | </div> | ||||
{% endblock %} | |||||
{% endblock %} | |||||
<style> | |||||
.test{ | |||||
background-color: darkgray; | |||||
} | |||||
</style> |
{% extends 'base.html' %} | |||||
{% block title %} | |||||
Login | |||||
{% endblock %} | |||||
{% block content %} | |||||
{% if next %} | |||||
{% if user.is_authenticated %} | |||||
<p>Your account doesn't have access to this page. To proceed, | |||||
please login with an account that has access.</p> | |||||
{% else %} | |||||
<p>Please login to see this page.</p> | |||||
{% endif %} | |||||
{% endif %} | |||||
<form method="post" action="{% url 'login' %}"> | |||||
{% csrf_token %} | |||||
<div> | |||||
<tb>{{ form.username.label_tag }}</tb> | |||||
<p>{{ form.username }}</p> | |||||
</div> | |||||
<div> | |||||
<p>{{ form.password.label_tag }}</p> | |||||
<p>{{ form.password }}</p> | |||||
</div> | |||||
<div> | |||||
<input type="submit" value="login"/> | |||||
<input type="hidden" name="next" value="{{ next }}"/> | |||||
</div> | |||||
</form> | |||||
{% endblock %} |
path('home', views.welcome_seite), | path('home', views.welcome_seite), | ||||
path('about', views.about_seite), | path('about', views.about_seite), | ||||
url(r'^new', views.new, name='new'), | url(r'^new', views.new, name='new'), | ||||
path('delete/<int:deleteId>', views.delete, name ='delete') | |||||
path('delete/<int:deleteId>', views.delete, name ='delete'), | |||||
path('notices', views.notice_list), | |||||
path('notices/<int:id>', views.notice_detail) | |||||
] | ] |
from django.http import HttpResponse | |||||
from django.contrib.admin.views.decorators import staff_member_required | |||||
from django.contrib.auth.decorators import login_required | |||||
from django.http import HttpResponse, JsonResponse | |||||
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 rest_framework.parsers import JSONParser | |||||
from posts.forms import NoticeForm | from posts.forms import NoticeForm | ||||
from posts.models import Notice | from posts.models import Notice | ||||
# Create your views here. | # Create your views here. | ||||
from posts.serializers import NoticeSerializer | |||||
def index(request): | def index(request): | ||||
notices = Notice.objects.all() | notices = Notice.objects.all() | ||||
notices = notices.filter(pub_start__lte=timezone.now()) | notices = notices.filter(pub_start__lte=timezone.now()) | ||||
def about_seite(request): | def about_seite(request): | ||||
return render(request, 'posts/about.html') | return render(request, 'posts/about.html') | ||||
@login_required | |||||
def new(request): | def new(request): | ||||
if request.method == "POST": | if request.method == "POST": | ||||
form = NoticeForm(request.POST) | form = NoticeForm(request.POST) | ||||
return render(request, 'posts/edit.html', context) | return render(request, 'posts/edit.html', context) | ||||
@staff_member_required | |||||
def delete(request, deleteId = None): | def delete(request, deleteId = None): | ||||
if deleteId != None: | if deleteId != None: | ||||
delNotice = Notice.objects.get(id=deleteId) | delNotice = Notice.objects.get(id=deleteId) | ||||
if delNotice != None: | if delNotice != None: | ||||
delNotice.delete() | delNotice.delete() | ||||
return redirect('index') | return redirect('index') | ||||
@csrf_exempt | |||||
def notice_list(request): | |||||
if request.method == 'GET': | |||||
notices = Notice.objects.all() | |||||
serializer = NoticeSerializer(notices, many=True) | |||||
return JsonResponse(serializer.data, safe=False) | |||||
elif request.method=='POST': | |||||
data = JSONParser().parse(request) | |||||
serializer = NoticeSerializer(data=data) | |||||
if serializer.is_valid(): | |||||
serializer.save() | |||||
return JsonResponse(serializer.data, status=201) | |||||
return JsonResponse(serializer.errors, status=201) | |||||
@csrf_exempt | |||||
def notice_detail(request, id): | |||||
try: | |||||
notice= Notice.objects.get(id =id) | |||||
except Notice.DoesNotExist: | |||||
return HttpResponse(status=404) | |||||
if request.method =='GET': | |||||
serializer = NoticeSerializer(notice) | |||||
return JsonResponse(serializer.data) | |||||
elif request.method =='PUT': | |||||
data = JSONParser().parse(request) | |||||
serializer = NoticeSerializer(notice, data=data) | |||||
if serializer.is_valid(): | |||||
serializer.save() | |||||
return JsonResponse(serializer.data) | |||||
return JsonResponse(serializer.errors, status=400) | |||||
elif request.method == 'DELETE': | |||||
notice.delete() | |||||
return HttpResponse(status=204) | |||||
<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://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> | <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> | ||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> | <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> | ||||
<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> | |||||
<title>{% block title %}First Django Application{% endblock %}</title> | <title>{% block title %}First Django Application{% endblock %}</title> | ||||
</head> | </head> | ||||
<body> | <body> | ||||
<nav> | <nav> | ||||
{% block nav %} | {% block nav %} | ||||
<nav class="navbar navbar-expand-sm bg-dark navbar-dark"> | |||||
<nav class="navbar navbar-expand-sm bg-info navbar-dark"> | |||||
<ul class="navbar-nav"> | <ul class="navbar-nav"> | ||||
<li class="nav-item"> | <li class="nav-item"> | ||||
<a class="nav-link" href="/posts/home">Home</a> | <a class="nav-link" href="/posts/home">Home</a> | ||||
</li> | </li> | ||||
<li class="nav-item"> | <li class="nav-item"> | ||||
<a class="nav-link" href="https://www.google.de/?hl=de">google</a> | |||||
<a class="nav-link" href="https://www.google.de/?hl=de" target="_blank">google</a> | |||||
</li> | </li> | ||||
<li class="nav-item"> | <li class="nav-item"> | ||||
<a class="nav-link" href="https://www.th-nuernberg.de/fakultaeten/efi/">Efi</a> | |||||
<a class="nav-link" href="https://www.th-nuernberg.de/fakultaeten/efi/" target="_blank">Efi</a> | |||||
</li> | </li> | ||||
<li class="nav-item"> | <li class="nav-item"> | ||||
<a class="nav-link" href="/posts/about">about</a> | <a class="nav-link" href="/posts/about">about</a> | ||||
<a class="nav-link" href="/posts/notice">notice</a> | <a class="nav-link" href="/posts/notice">notice</a> | ||||
</li> | </li> | ||||
</ul> | </ul> | ||||
</nav> | </nav> | ||||
{% endblock %} | {% endblock %} | ||||
</nav> | </nav> |