Solution of 08
This commit is contained in:
parent
5d99369516
commit
6a55fa7394
@ -37,7 +37,8 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'posts.apps.PostsConfig' #you could also add 'posts' to register everything in your app
|
'posts.apps.PostsConfig', #you could also add 'posts' to register everything in your app
|
||||||
|
'rest_framework'
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
7
posts/serializers.py
Normal file
7
posts/serializers.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from .models import Notice
|
||||||
|
|
||||||
|
class NoticeSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Notice
|
||||||
|
fields = ('id', 'notice_title', 'notice_text', 'pub_start', 'pub_end')
|
@ -4,8 +4,8 @@ from django.urls import path, re_path
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# use re_path for regex (.*) - this regex accepts anything - see https://docs.python.org/3/library/re.html for more
|
# use re_path for regex (.*) - this regex accepts anything - see https://docs.python.org/3/library/re.html for more
|
||||||
path('', views.home, name='index'),
|
path('', views.home, name='index'),
|
||||||
re_path('delete/(?P<value>\d+)/$', views.delete, name='delete'),
|
path('delete/<int:value>', views.delete, name='delete'),
|
||||||
re_path('(?P<value>.*)/$', views.home, name='textausgabe'),
|
|
||||||
path('new', views.new, name='new'),
|
path('new', views.new, name='new'),
|
||||||
# only accept numbers ^[0-9]*$
|
path('notices/', views.notice_list, name='index'),
|
||||||
|
path('notices/<int:id>', views.notice_detail, name='delete'),
|
||||||
]
|
]
|
@ -1,10 +1,15 @@
|
|||||||
|
from django.http import JsonResponse, HttpResponse
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from rest_framework.parsers import JSONParser
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
|
||||||
from posts.models import Notice
|
from posts.models import Notice
|
||||||
from posts.forms import NoticeForm
|
from posts.forms import NoticeForm
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.contrib.admin.views.decorators import staff_member_required
|
from django.contrib.admin.views.decorators import staff_member_required
|
||||||
|
from posts.serializers import NoticeSerializer
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
|
||||||
|
|
||||||
@ -40,4 +45,44 @@ def new(request):
|
|||||||
def delete(request, value):
|
def delete(request, value):
|
||||||
notice = Notice.objects.get(id = value)
|
notice = Notice.objects.get(id = value)
|
||||||
notice.delete()
|
notice.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=400)
|
||||||
|
return HttpResponse(status=405)
|
||||||
|
|
||||||
|
@csrf_exempt
|
||||||
|
def notice_detail(request, id):
|
||||||
|
try:
|
||||||
|
notice = Notice.objects.get(id=id)
|
||||||
|
except:
|
||||||
|
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)
|
||||||
|
|
||||||
|
return HttpResponse(status=405)
|
Loading…
x
Reference in New Issue
Block a user