【问题标题】:Upload and Download excel file in Django在 Django 中上传和下载 excel 文件
【发布时间】:2018-10-13 14:57:02
【问题描述】:

我想创建一个网络框架我想在其中上传 excel 文件,然后保存并下载相同的 excel 文件。如果在下载此文件时将相同的 excel 文件上传 2 次或 3 次或更多次,应该用一些数字来命名?有一个下载按钮我怎么能这样做

我已经编写了上传代码,但我不确定如何继续下载部分,谁能帮忙

Views.py

from django.shortcuts import render
import openpyxl
import settings

def index(request):
    if "GET" == request.method:
        return render(request, 'myapp/index.html', {})
    else:
        excel_file = request.FILES["excel_file"]

        # you may put validations here to check extension or file size

        wb = openpyxl.load_workbook(excel_file)

        # getting all sheets
        sheets = wb.sheetnames
        print(sheets)

        # getting a particular sheet
        worksheet = wb["Sheet1"]
        print(worksheet)

        # getting active sheet
        active_sheet = wb.active
        print(active_sheet)

        # reading a cell
        print(worksheet["A1"].value)

        excel_data = list()
        # iterating over the rows and
        # getting value from each cell in row
        for row in worksheet.iter_rows():
            row_data = list()
            for cell in row:
                row_data.append(str(cell.value))
                print(cell.value)
            excel_data.append(row_data)

        return render(request, 'myapp/index.html', {"excel_data":excel_data})

索引.html

<html>
    <head>
        <title>
            Excel file upload and processing : Django Example : ThePythonDjango.Com
        </title>
    </head>
    <body style="margin-top: 30px;margin-left: 30px;">
        <form action="{% url "myapp:index" %}" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            <input type="file"
                   title="Upload excel file"
                   name="excel_file"
                   style="border: 1px solid black; padding: 5px;"
                   required="required">
            <p>
            <input type="submit"
                   value="Upload"
                   style="border: 1px solid green; padding:5px; border-radius: 2px; cursor: pointer;">
        </form>

        <p></p>
        <hr>

        {% for row in excel_data %}
            {% for cell in row %}
                {{ cell }}&nbsp;&nbsp;
            {% endfor %}
            <br>
        {% endfor %}
    </body>
</html>

urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls', namespace="myapp")),
]

【问题讨论】:

    标签: python django download upload


    【解决方案1】:

    当多次上传同一个文件时,新文件将以扩展数字或随机字符串命名。你可以调整它。

    有两种方法可以使文件可供下载:

    1. 将您上传的文件存储在公共场所,然后使用网络服务器(nginx、apache)处理下载。就像您使用网络服务器提供 Django 项目的静态/媒体文件一样。推荐。看看这个:Deploying static files

    2. 在 django 视图中,读取文件的内容,然后将其返回给客户端。不推荐。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 2021-02-16
      • 2021-03-13
      • 1970-01-01
      • 2018-09-05
      • 2021-06-02
      • 1970-01-01
      相关资源
      最近更新 更多