Hello, i’m trying to developp my first Django project and i’m actually stuck with an error.
So i have on my HTML template a button to load a xls file, and a submitting button that i want it to start an external python script using this file.
Here is my project:
home.html
<h1>Home</h1>
<form action="/external/" method="post" enctype="multipart/form-data">
{% csrf_token %}
Input xls file :<br><br>
<input type="file" name="InputFile" accept=".xls" required><br>
<input type="submit" value="execute python script"><br>
</form>
views.py
from subprocess import run, PIPE
import sys
def home(request):
return render(request, 'home.html')
def external(request):
inp=request.FILES.get('InputFile')
run([sys.executable,r'pathToMyDjangoProject\script.py', inp], shell=False,stdout=PIPE)
return render(request,'home.html')
urls.py
urlpatterns = [
path(r'', views.home),
path('external/', views.external),
]
script.py
def process(excelFile):
rdr = pd.read_excel(excelFile, header=None).values.tolist()
for row in rdr:
...
Finally the html page seems to work, le type of inp is a django InMemoryUploadedFile so i guess it’s correctly loaded but the error i have is :
TypeError at /external/
expected str, bytes or os.PathLike object, not InMemoryUploadedFile
.
|Request Method:|POST|
|Request URL:|http://127.0.0.1:8000/external/|
|Django Version:|3.2.7|
|Exception Type:|TypeError|
|Exception Value:|expected str, bytes or os.PathLike object, not InMemoryUploadedFile|
I think to understand there is a problem with variable’s type in the run() function but do you have an idea to correct it ?
Thank you by advance,