Browse Source

Praktikum8

master
parent
commit
76d25a6370
5 changed files with 45 additions and 3 deletions
  1. BIN
      news1/db.sqlite3
  2. 1
    0
      news1/news1/settings.py
  3. 2
    0
      news1/news1/urls.py
  4. 2
    0
      news1/posts/urls.py
  5. 40
    3
      news1/posts/views.py

BIN
news1/db.sqlite3 View File


+ 1
- 0
news1/news1/settings.py View File

@@ -38,6 +38,7 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'posts',
'rest_framework',
]

MIDDLEWARE = [

+ 2
- 0
news1/news1/urls.py View File

@@ -18,5 +18,7 @@ from django.urls import path, include
urlpatterns = [
path('posts/' , include('posts.urls')),
path('admin/', admin.site.urls),
path('accounts/' , include('django.contrib.auth.urls')),


]

+ 2
- 0
news1/posts/urls.py View File

@@ -6,4 +6,6 @@ urlpatterns =[
path('', views.index, name='index'),
path('new', views.new, name='new'),
path('delete/<int:deleteId>', views.delete, name='delete'),
path('notices/', views.notice_list),
path('notices/<int:id>/', views.notice_detail),
]

+ 40
- 3
news1/posts/views.py View File

@@ -1,9 +1,14 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser

from posts.forms import NoticeForm
from posts.serializers import NoticeSerializer
from.models import Notice
from django.utils import timezone
from django.http import HttpResponse
from django.http import HttpResponse, JsonResponse


def index(request):
notices= Notice.objects.all()
@@ -12,7 +17,7 @@ def index(request):
context={"notices":notices}
return render(request, 'posts/notice.html', context)

@login_required
def new(request):
if request.method == "POST":
form = NoticeForm(request.POST)
@@ -31,4 +36,36 @@ def delete(request, deleteId=None):
delNotice= Notice.objects.get(id=deleteId)
if delNotice!= None:
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)

Loading…
Cancel
Save