gpt4 book ai didi

python - URL 文件夹系统 Django Python

转载 作者:行者123 更新时间:2023-11-28 18:03:08 25 4
gpt4 key购买 nike

如何像在 Dropbox 中一样使用 url 在文件夹中移动?

示例:我有一个文件“site_name/home/path1/path2/file”的 url,如何在 Django 中将“path1/path2/file”作为 url 的参数?

或者是使用 GET 参数作为 PATH 文件“site_name/home?path=path1/path2/file”的唯一方法?

最佳答案

如果您使用的是 django 2.0+:

re_path(r'^.*', some_view)

否则:

url(r'^.*', some_view)

你应该把这个放在所有其他 url 之后,否则它们将停止工作,因为这个模式匹配每个 url。

然后你在 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]

对于像 site.com/home/path1/path2/path3/file/ 这样的路径,如果你打印 folders,你会得到这个:

['path1', 'path2', 'path3']

关于python - URL 文件夹系统 Django Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54957600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com