Пример #1
0
def name_detail(request, pk):
    if request.user.is_authenticated:

        # Tìm các names theo pk (id)
        try:
            name = Name.objects.get(pk=pk)

            #Tìm một name theo id
            if request.method == 'GET':
                name_serializer = NameSerializer(name)
                return JsonResponse(name_serializer.data)

            #Update một name theo id
            elif request.method == 'PUT':
                name_data = JSONParser().parse(request)
                name_serializer = NameSerializer(name, data=name_data)
                if name_serializer.is_valid():
                    name_serializer.save()
                    return JsonResponse(name_serializer.data,
                                        status=status.HTTP_200_OK)
                return JsonResponse(name_serializer.errors,
                                    status=status.HTTP_400_BAD_REQUEST)

            #Xoá một name theo id
            elif request.method == 'DELETE':
                name.delete()
                return JsonResponse(
                    {'message': 'Name was deleted successfully!'},
                    status=status.HTTP_204_NO_CONTENT)

        except Name.DoesNotExist:
            return JsonResponse({'message': 'The name does not exist'},
                                status=status.HTTP_404_NOT_FOUND)
    return redirect('login/')
Пример #2
0
def name_list(request):
    # GET danh sách các name,
    if request.method == 'GET':
        names = Name.objects.all()
        # print(names)
        lastname = request.GET.get('lastname', None)
        if lastname is not None:
            print(request.GET)
            names = names.filter(
                lastname__icontains=lastname).order_by('lastname')

        names_serializer = NameSerializer(names, many=True)
        return JsonResponse(names_serializer.data, safe=False)
        # return get_paginated_queryset_response(names, request)

    # POST một name mới
    elif request.method == 'POST':
        name_data = JSONParser().parse(request)
        user = Name.objects.create(id=request.data.get('id'),
                                   fullname=request.data.get('fullname'),
                                   menh=request.data.get('menh'),
                                   van=request.data.get('van'),
                                   gioitinh=request.data.get('gioitinh'),
                                   lastname=request.data.get('lastname'),
                                   meaning=request.data.get('meaning'),
                                   likes=request.data.get('likes'))
        print(user.fullname)
        name_serializer = NameSerializer(data=name_data)
        if name_serializer.is_valid():
            name_serializer.save()
            return JsonResponse(name_serializer.data,
                                status=status.HTTP_201_CREATED)
        return JsonResponse(name_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)