Hi, I am trying to create a form where the User fills in a few data in the form and then clicks a button(say ‘browse for files’) and that should pop up another form where the user can choose the server-side files. In the end, I need the file path the User chooses in that form, which I will then use to do further processing.
So far, I have defined my models with a FileField. But using that, it only shows the client side files which doesn’t meet my requirement. So I plan to create a button and using javascript onclick, open a pop-up form.
The pop-up form needs to navigate through the directories and track the User’s chosen file with the path. And this path is then passed to the main form and then it is submitted so that I can perform the next steps.
I have used this as a template, but finding it hard to translate to Django runnable one. (Simple Folder tree with PHP and jQuery)
I am a newbie to Django, javascript, and Ajax. Any help on this is appreciated.
Views.py:
def file_browserJSON(request):
context = ''
root_directory = os.path.join('path')
files = []
folders = []
if os.path.exists(root_directory):
if( root_directory[ len(root_directory) - 1 ] == '/' ):
folders = root_directory
else:
folders = root_directory+'/'
dir1 = dir(root_directory)
for f in os.walk(root_directory):
files.append(f)
if( len(files) > 2): #First 2 entries are . and .. -skip them
print(files)
files.sort()
print(files)
context = '<ul class="filetree" style="display: none;">'
for dirpath, dirs, f in os.walk(root_directory):
print(dirpath)
print (dirs)
print(f)
context += '<li class="folder collapsed"><a href="#" rel="'+ str(dirpath)+'/">' + str(dirs) + '</a></li>'
for file in f:
ext = re.sub('/^.*\./', '', file)
context += '<li class="file ext_'+ext+'"><a href="#" rel="' +dirpath+'">'+file+'</a></li>'
context += '</ul>'
return JsonResponse({'context':context})
forms.py:
from django import forms
from django.db import models
from .models import Whisper
class WhisperForm(forms.ModelForm):
class Meta:
model = Whisper
fields = '__all__'
labels = {
'name': 'Name for this command run ',
'model': 'Model to use ',
'output_format': 'Format of the output file ',
'task': 'Whether to perform speech recognition or translation ',
'language': 'Language spoken in the audio ',
'upload_file': 'Choose the file here ' //Need to add a button here
}
Filebrowder.html:
Javascript call (which I have created a separate form as I am not sure how to modify forms.py):
<script type="text/javascript" src="whisper/static/js/filebrowser.js" >
</script>
</head>
<body>
<div id="container"> </div>
<div id="selected_file"></div>
</body>
filebroswer.js:
const containerBox = document.getElementById('container')
containerBox.innerHTML = '<ul class="filetree start"><li class="wait">' + 'Generating Tree...' + '<li></ul>'
const selectedFile = document.getElementById('selected_file')
selectedFile.innerHTML = '<ul class="filetree start"><li class="wait">' + 'Generating Tree...' + '<li></ul>'
$.ajax {
type: 'GET'
url: '/filebrowserJSON',
success: function (response) {
console.log('success', response.context)
const data = response.context
console.log(data)
data.forEach(el=> {
const entry = $(".class:contains('folder')" );
containerBox.innerHTML += `
${} // #Incomplete The data needs to be modified here such that the user navigation through folders is tracked.
});
},
error: function(error) {
console.log('error', error)
}
}