或者是使用 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): 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']