私はDjangoのアプリに取り組んでいます
取得するために defaultlist を使用しました: Key: -value -value -value Key2: -value -value value
しかし、出力に何もなく、ホームページを pdf にダウンロードしたいのですが、次のエラーが表示されます。
"/views.download_my_pdf の AttributeError 'str' オブジェクトに属性 'read' がありません"
どこで間違ったのですか?ご協力ありがとうございました
Django の views.py
from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
import pandas as pd
from django.http import HttpResponse
from wsgiref.util import FileWrapper
def home(request):
if request.method == 'POST':
uploaded_file = request.FILES['document']
uploaded_file2 = request.FILES['document2']
if uploaded_file.name.endswith('.xls'):
savefile = FileSystemStorage()
name = savefile.save(uploaded_file.name, uploaded_file)
name2 = savefile.save(uploaded_file2.name, uploaded_file2)
d = os.getcwd()
file_directory = d+'\\media\\'+name
file_directory2 = d+'\\media\\'+name2
results,output_df =results1(file_directory,file_directory2)
return render(request,"results.html", {"results":results,"output_df":output_df,})
return render(request, "index.html")
def readfile(uploaded_file):
data = pd.read_excel(uploaded_file, index_col=None)
return data
def results1(file1,file2):
results_list = defaultdict(list)
names_loc = file2
listing_file = pd.read_excel(file1, index_col=None)
for x in date_index:
if time>2h:
results_list["List of samples better"].append(sample_id)
if len(u_index) > 1:
results_list["List of Samples "].append(sample_id)
output_df = output_df.append(row_to_add, ignore_index=True)
else:
results_list["List of Samples not in the listing file"].append(sample_name[0])
except:
print('The template name isnt good: {}'.format(sample_id))
return results_list, output_df.to_html()
def download_pdf(request):
filename = 'faults.pdf'
content = FileWrapper(filename)
response = HttpResponse(content, content_type='application/pdf')
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = 'attachment; filename=%s' % 'faults.pdf'
return response
.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Dashboard Result</title>
</head>
<body style="margin-top: 30px; padding: 100px">
<a class="btn btn-primary" href="{% url 'download_pdf' %}" role="button">Download pdf</a>
{% for key, value in results1.items %}
<tr>
<td>{{ key }} </td>
<td>{{ value }} </td>
</tr>
{% endfor %}
</body>
</html>
django での私のアプリの urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
path('views.download_my_pdf', views.download_pdf, name="download_pdf"),
]