URL 文件夹系统 Django Python

如何像 Dropbox 一样使用 url 浏览文件夹?

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

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


慕妹3242003
浏览 162回答 1
1回答

jeck猫

如果您使用的是 django 2.0+:re_path(r'^.*', some_view)除此以外:url(r'^.*', some_view)你应该把它放在所有其他 url 之后,否则它们将停止工作,因为这个模式匹配每个 url。然后你会得到你认为的路径:def some_view(request):&nbsp; &nbsp; full_path = request.path&nbsp; &nbsp; split_path = full_path.split('/')&nbsp; &nbsp; # If you have slash at the end of the url, you should pick the second last item.&nbsp; &nbsp; if len(split_path[-1] < 1:&nbsp; &nbsp; &nbsp; &nbsp; file = split_path[-2]&nbsp; &nbsp; &nbsp; &nbsp; folders = split_path[2:len(split_path)-2]&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; file = split_path[-1]&nbsp; &nbsp; &nbsp; &nbsp; folders = split_path[2:len(split_path)-1]对于像这样的路径,site.com/home/path1/path2/path3/file/如果你打印,你会得到这个folders:['path1', 'path2', 'path3']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python