views.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. from django.http import HttpResponse, JsonResponse
  2. from django.views.decorators.csrf import csrf_exempt
  3. from django.shortcuts import render
  4. from rest_framework.parsers import JSONParser
  5. from .models import Task, Project
  6. from .serializers import ProjectSerializer
  7. def dashboard(request):
  8. """Display main information, provide a way to reach rest"""
  9. tasks = Task.objects.order_by('-created_at')[:5]
  10. context = {'latest_task_list': tasks}
  11. return render(request, 'storage/index.html', context)
  12. @csrf_exempt
  13. def project_list(request):
  14. if request.method == 'GET':
  15. projects = Project.objects.all()
  16. serializer = ProjectSerializer(projects, many=True)
  17. return JsonResponse(serializer.data, safe=False)
  18. elif request.method == 'POST':
  19. data = JSONParser().parse(request)
  20. serializer = ProjectSerializer(data=data)
  21. if serializer.is_valid():
  22. serializer.save()
  23. return JsonResponse(serializer.data, status=201)
  24. return JsonResponse(serializer.errors, status=400)