I have spent a LOT of time trying to resolve this- Read Django docs, consulted forms but not got anything satisfactory. So please be patient here with me. I am trying to do an upload of an Image file here from my html template The file upload happens properly an d I am able to see the uploaded image file in the HTML.
In my views.py,
views.py
from django.shortcuts import render
import requests
import sys
from subprocess import run,PIPE
from django.core.files.storage import FileSystemStorage
def button(request):
return render(request,'home.html')
def output(request):
data=requests.get("https://www.google.com/")
print(data.text)
data=data.text
return render(request,'home.html',{'data':data})
def external(request):
inp= request.POST.get('param')
image=request.FILES['image']
print("image is ",image)
fs=FileSystemStorage()
filename=fs.save(image.name,image)
fileurl=fs.open(filename)
templateurl=fs.url(filename)
print("file raw url",filename)
print("file full url", fileurl)
print("template url",templateurl)
out= run([sys.executable,'D:/corona/Image Edit Html Button Run Python Script/Image Edit Html Button Run Python Script/test.py',inp],shell=False,stdout=PIPE)
image= run([sys.executable,'D:/corona/Image Edit Html Button Run Python Script/Image Edit Html Button Run Python Script/image.py',str(fileurl),str(filename)],shell=False,stdout=PIPE)
print(out)
print(image.stdout)
return render(request,'home.html',{'data':out.stdout,'raw_url':templateurl,'edit_url':image.stdout})
In my home.html
home.html
<!DOCTYPE html>
<html>
<head>
<title>
Python button script
</title>
</head>
<body>
<button onclick="location.href='{% url 'script' %}'">Execute Script</button> <hr>
{% if data %}
{{data | safe}}
{% endif %}
<br><br>
{% if raw_url or edit_url %}
<span>
RAW IMAGE:
<img src={{raw_url}} height=500 width=500>
PROCESSED IMAGE:
<img src={{edit_url}} height=500 width=500>
</span>
{% endif %}
<br><br>
<form action="/external/" method="post" enctype="multipart/form-data">
{% csrf_token %}
Input Text:
<input type="text" name="param" required><br><br>
<br><br>
<input type="file"name="image" id="image" required>
<br><br>
<input type="submit" value="Execute External Python Script">
</form>
</body>
</html>