Path duplication in href

I'm new to Django and I'm trying to create web viewer for myself that browses static folder to open subfolders and .mp4 files. Problem occurs when I open second child of static folder: links break and start duplicating path.

Example:

Static folder structure:

- static folder
    - folder
    - videos
        -videos_1
            -videos_1_1
            -videos_1_2
        -videos_2

When root and first child folders are viewed, all HTML links work as needed and lead to next subfolders or files (links look like localhost:8000/videos/videos_1). When second child folder is viewed, it's content is displayed, but all links are broken, for example when folder videos_1 is viewed, links look like localhost:8000/videos/videos/videos_1/videos_1_1, however, when HTML is inspected, link is <a href="videos/videos_1/videos_1_1"></a>.

urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', folderViewer, name='files'),
    path('<path:kw>', folderViewer),]

views.py:

def folderViewer(request, kw=''):

    path = os.path.join(STATIC_URL, kw)

    if os.path.isdir(path):
        files = os.listdir(path)
        paths_files = []
        for i in range(len(files)):
            paths_files.append([kw + '/' + files[i], files[i]])
        context = {
            'files': files,
            'paths_files': paths_files,
        }

        return render(request, 'files.html', context)

files.html:

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>files</title>
</head>
<body>
    {% for i in paths_files %}
        <div>
            <a href="{{ i.0 }}">{{ i.1 }}</a>
        </div>
    {% endfor %}
</body>
</html>
Back to Top