1

I want to make Django accept url that consists of infinite number of parameters. Something like dropbox has going on. Each parameter for each folder the file is in. There could be infinite number of subfolders. Parameter is alphanumeric.

1
  • You can try with the request.GET object in your view function, without defining any parameter. request.GET.values() or request.GET.items() Commented Aug 5, 2019 at 18:21

2 Answers 2

5

You can create a URL that accepts an arbitrarily long parameter than contains some form of separator and then in your view split the parameter by this separator. For a path like parameter:

url(r'^prefix/(?P<path>[a-zA-Z\/]*)/$', your_view),

def your_view(request, path):
    folders = path.split('/')

Now any request to this URL like prefix/folder1/folder2/folder3/ and folders will contain ['folder1', 'folder2', 'folder3']

Sign up to request clarification or add additional context in comments.

2 Comments

thank you! That was my first thought, but i saw dropbox doing it, so i thought there had to be a way. Well that should work as well!
let me ask you, with what character does your regex split folders
0

I don't think so that you can define infinite url parameters with django URLS. Because with django you have to declare the urls that you site will gonna use.

But if you are talking specifically of folder and files, you can do this with FileField in a model, that allows you to decide where you can save files and save the path in instance.filefield.url. then insted of search django urls, search nginx url, something like this. {site_url}/static/{any}/{folder}/{you}/{want}/{etc}/.

Inclusive, you can use models like this.

class Folder(models.Model):
    name = models.CharField(...)
    parent = mdoels.ForeignKey('self')
    ...

    def get_path_to_here(self):
        # here you have the full url getting all parents folders.
        ...


def func_to_declare_where_save_it(instance, filename):
    return os.path.join(
        f'{instance.folder.get_path_to_here()}',
        f'{filename}')


class File(models.Model):
   name = models.CharField(...)
   folder = models.ForeignKey(Folder)
   file = models.FileField(upload_to=func_to_declare_where_save_it)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.