Cannot list the content of a subdirectory of static in my django projet

14 views
Skip to first unread message

Guy NANA

unread,
Jan 27, 2020, 9:21:57 AM1/27/20
to Django users
I have in my project a directory named uts in static/ folder that I want to list the content and send it in HttpResponse but the glob.glob() function doesn't work.
Here you have the structure of my project. The utility function list_dir_content which call glob.glob is located in the utils folder: utils/data.py
import glob
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


# def get_remote_dataset():

# def get_local_dataset():


def list_dir_content(dir_path, file_extension=''):
files_pattern = dir_path + '/*.' + file_extension
print("files_pattern : ", files_pattern)
#files_results_paths = glob.glob(files_pattern)
files_results_paths = glob.glob('../tsanalysisapp/static/tsanalysisapp/datasets/uts/*.')
print("files_results_paths : ", files_results_paths)
return files_results_paths
## in models.py
from django.db import models
from django.conf import settings

# utils methods import
import utils.data as data
import os
from django.conf import settings

# Create your models here.
class Dataset(models.Model):
"""docstring for dataset"""
dataset_path = models.CharField(max_length=256)
dataset_name = models.CharField(max_length=256)
dataset_nb_instances = models.CharField(max_length=256)
dataset_type = models.CharField(max_length=3)
dataset_adding_date = models.DateField()

def __str__(self):
return "dataset_path : {0}  - dataset_name : {1} - number of instances : {2}".format(self.dataset_path, self.dataset_name, self.dataset_nb_instances)
@classmethod
def get_uts_datasets(cls):
#uts_datasets_path = os.path.join(settings.DATASETS_DIR, 'uts')
#print("pathname of uts datasets repositorie : ", uts_datasets_path)
uts_datasets_files = data.list_dir_content(settings.UTS_DATASETS_DIR)
print("UTS list of files : ", uts_datasets_files)
uts_datasets = []
for uts_datasets_file in uts_datasets_files:
dataset_type = 'uts'
dataset_path = uts_datasets_file
dataset_name = data.get_dataset_name(uts_datasets_file)
dataset_nb_instances = data.get_nb_instances(uts_datasets_file)
uts_dataset = Dataset(dataset_path = dataset_path, dataset_name = dataset_name, dataset_nb_instances = dataset_nb_instances, dataset_type = dataset_type)
uts_datasets.append(uts_dataset)
print("UTS datasets : ", uts_datasets)

return uts_datasets

@classmethod
def get_mts_datasets(cls):
mts_datasets_path = settings.DATASETS_DIR + '/mts'
mts_datasets_files = data.list_dir_content(mts_datasets_path)
mts_datasets = []
for mts_datasets_file in mts_datasets_files:
dataset_type = 'mts'
dataset_path = mts_datasets_file
dataset_name = data.get_dataset_name(mts_datasets_file)
dataset_nb_instances = data.get_nb_instances(mts_datasets_file)
mts_dataset = Dataset(dataset_path = dataset_path, dataset_name = dataset_name, dataset_nb_instances = dataset_nb_instances, dataset_type = dataset_type)
mts_datasets.append(mts_dataset)

return mts_datasets

## in views.py
def server_uts_datasets(request):
if request.method == 'GET':
uts_datasets = Dataset.get_uts_datasets()
uts_datasets_serializer = DatasetSerializer(uts_datasets, many=True)
print(uts_datasets)
return JsonResponse(uts_datasets_serializer.data, safe=False)

Thanks for your kindly help in advance...

gjangoproject_structure.JPG

data.py
views.py
models.py
serializers.py
Reply all
Reply to author
Forward
0 new messages