1 回答

TA貢獻(xiàn)1909條經(jīng)驗 獲得超7個贊
如果您使用的是 django 2.0+:
re_path(r'^.*', some_view)
除此以外:
url(r'^.*', some_view)
你應(yīng)該把它放在所有其他 url 之后,否則它們將停止工作,因為這個模式匹配每個 url。
然后你會得到你認(rèn)為的路徑:
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']
添加回答
舉報