Получить словарную переменную, чтобы использовать ее в том же словаре для получения расширения файла django python

Я создаю дерево папок для его отображения. Для этого мне нужно получить расширение файла. Перед папкой нужно ввести пароль.

my views.py `

def www_telechargements(request):
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = CatalogueForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            context = {
                'mdp': form.cleaned_data["response_mdp"]
            }
            good_answer = ("\n".join(context.values()))
            if good_answer == "Vitasolar1234":
                template = loader.get_template('telechargement-response.html')
                arborescence_path = os.path.join(BASE_DIR, "WWW/static/arborescence")
                arborescence_content = os.listdir(arborescence_path)
                response_gabarit = {
                    'answer': True,
                    'dossier': arborescence_content,
                    'type': pathlib.Path(os.listdir(response_gabarit('dossier')).suffix)#c'est ici que je veux récupérer le string de 'dossier'
                    }
                print(os.listdir(arborescence_path))
                return HttpResponse(template.render(response_gabarit, request))
            else:
                response_gabarit = {'answer': False}
                template = loader.get_template('telechargement-response.html')
                return HttpResponse(template.render(response_gabarit, request))

        # if a GET (or any other method) we'll create a blank form
    else:
        form = CatalogueForm()

    return render(request, 'telechargements.html', {'form': form})

это моя ошибка : 'type': pathlib.Path(os.listdir(response_gabarit('dossier')).suffix) UnboundLocalError: локальная переменная 'response_gabarit' ссылается перед присвоением

>

Я хотел бы получить строку 'досье', но это невозможно

Я понимаю свою ошибку, но я понятия не имею, как решить эту проблему

Я пытался использовать мою переменную 'content_tree' напрямую, но это список, а моему методу нужна строка .

Вернуться на верх