[Django]-How to list all the files of a custom directory

0๐Ÿ‘

โœ…

So here is my solution:

def showDocuments(request, *args, **kwargs):
    """ shows all the files of the requested course  """
    crs_names = CoursesNames.objects.all()
    context = {
        "crs_names": crs_names,
        "files": [],
        "not_found" : "",
    }
    if request.POST:

        file_name = request.POST["course"]
        not_found = "There are no files for {0} yet!".format(file_name)
        documents = Document.objects.all().filter(course_name__courses__iexact=file_name)
        context["documents"] = documents
        context["filename"] = file_name

        documentObjectList = Document.objects.all().values_list("document").filter(course_name__courses__iexact=file_name)
        queryset = CoursesNames.objects.filter(courses__exact=file_name).exists()

        if queryset:
            while len(context["files"]) > 0: context["files"].pop()
            for i in documentObjectList:
                context["files"] = i
                print(context["files"])
            if len(context["documents"]) == 0: context["not_found"] = not_found
    return render(request, 'dashboard.html', context)

Leave a comment