Praktikum 8
This commit is contained in:
parent
50073eef79
commit
85f2a7ca03
@ -32,6 +32,7 @@ ALLOWED_HOSTS = []
|
|||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
'posts.apps.PostsConfig',
|
'posts.apps.PostsConfig',
|
||||||
|
'rest_framework',
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
|
|||||||
8
news/posts/serializers.py
Normal file
8
news/posts/serializers.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
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,5 +4,8 @@ from . import views
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.index, name='posts'),
|
path('', views.index, name='posts'),
|
||||||
path('new', views.new, name='new'),
|
path('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),
|
||||||
]
|
]
|
||||||
@ -1,11 +1,15 @@
|
|||||||
import logging
|
import logging
|
||||||
from django.shortcuts import render, redirect, render_to_response
|
from django.shortcuts import render, redirect, render_to_response
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse, JsonResponse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
|
|
||||||
|
from rest_framework.renderers import JSONRenderer
|
||||||
|
from rest_framework.parsers import JSONParser
|
||||||
|
|
||||||
from .models import Notice
|
from .models import Notice
|
||||||
from .forms import NoticeForm
|
from .forms import NoticeForm
|
||||||
|
from .serializers import NoticeSerializer
|
||||||
|
|
||||||
logger = None
|
logger = None
|
||||||
|
|
||||||
@ -55,3 +59,39 @@ def delete(request, deleteId=None):
|
|||||||
return redirect('posts')
|
return redirect('posts')
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
Loading…
x
Reference in New Issue
Block a user