如何像 Dropbox 一样使用 url 浏览文件夹?示例:我有一个文件“site_name/home/path1/path2/file”的 url,如何将“path1/path2/file”作为 Django 中 url 的参数?或者是使用 GET 参数作为 PATH 文件“site_name/home?path=path1/path2/file”的唯一方法?
1 回答

jeck猫
TA贡献1909条经验 获得超7个赞
如果您使用的是 django 2.0+:
re_path(r'^.*', some_view)
除此以外:
url(r'^.*', some_view)
你应该把它放在所有其他 url 之后,否则它们将停止工作,因为这个模式匹配每个 url。
然后你会得到你认为的路径:
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']
添加回答
举报
0/150
提交
取消