Issue
My objective is to read the contents of a worksheet uploaded by the user and apply a function to it. So far the uploading works perfect and i see the file in the server. However every time i try to select the file from the list i keep getting this error:
Invalid file path or buffer object type: <class 'django.db.models.query.QuerySet'>
Right here is the view that handles the file selection the the template and returns an excel file where the data was processed to be downloaded (for example if i want to filter out all duplicates and return clean data so i upload the excel sheet and return the filtered excel file.):
# get file run func on it and return new excel file, view needs to handle url
def dataprocess(self, pk):
data = Worksheet.objects.filter(pk=pk)
df = pd.read_excel(data, engine='xlrd')
#finishdata = dataprocessor(df) // ignore this part for now.
print("HERE---------------------->", df.url)
# return excel file
response = HttpResponse(content_type='application/vnd.ms-excel')
# tell the browser what the file is named
response['Content-Disposition'] = 'attachment;filename="ezpassreport.xlsx"'
# put the spreadsheet data into the response
response.write(df.getvalue())
return response
This is the HTML Page:
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
Select Report
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
{% for i in worksheet %}
<a href="{% url 'ezpass:dataprocess' i.pk %}">{{i.worksheet}}</a><br>
{% endfor %}
</div>
And the urls just in case:
from ezpass.views import ezpass, file_upload, dataprocess
from django.urls import path
app_name = 'ezpass'
urlpatterns = [
path('ezpass/', ezpass, name="ezpass"),
path('upload/', file_upload, name='upload'),
path('dataprocess/<int:pk>', dataprocess, name='dataprocess')
]
Solution
This is how i fixed it.
#handles the download and data processing of the ezpass data
def dataprocess(self, pk):
data = Worksheet.objects.get(pk=pk)
path = data.worksheet.path
df = pd.read_excel(os.path.join(BASE_DIR, path), engine='openpyxl')
finishdata: pd = dataprocessor(df)
# return excel file
response = HttpResponse(content_type='application/vnd.ms-excel')
# tell the browser what the file is named
response['Content-Disposition'] = 'attachment;filename="ezpassreport.xlsx"'
# put the spreadsheet data into the response
response.write(finishdata)
return response
Answered By - Rodrigo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.