0

我正在 Django 上开发一个应用程序

我使用 defaultlist 来获取: Key: -value -value -value Key2: -value -value value

但我的输出中没有任何内容,我想将主页下载到 pdf ,但出现以下错误:

“/views.download_my_pdf 'str' 对象的 AttributeError 没有属性 'read'”

请问我哪里错了?谢谢您的帮助

Django 视图.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"),

]

4

0 回答 0