2

How is it possible to move through the folders using url like in Dropbox?

Example: I have an url to a file "site_name/home/path1/path2/file", how can I take the "path1/path2/file" as parameter from url in Django?

Or is the only way to use GET parameters as PATH to file "site_name/home?path=path1/path2/file"?

1 Answer 1

2

If you are using django 2.0+:

re_path(r'^.*', some_view)

Otherwise:

url(r'^.*', some_view)

You should put this after all other urls otherwise they'll stop working because this pattern matches every url.

And then you get the path in your view:

def some_view(request):
    full_path = request.path

    split_path = full_path.split('/')

    # If you have slash at the end of the url, you should pick the second last item.
    if len(split_path[-1] < 1:
        file = split_path[-2]
        folders = split_path[2:len(split_path)-2]
    else:
        file = split_path[-1]
        folders = split_path[2:len(split_path)-1]

For a path like site.com/home/path1/path2/path3/file/ you'll get this if you print folders:

['path1', 'path2', 'path3']
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, thank you for the help! It's possible to create DB model (File, Folder, User) and save files url and then navigate like this?
Yes but if you're going to use db for folders then just add slug for them so you can find them easier.

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.