Adi*_*tya 5 django nginx amazon-ec2 python-3.x gunicorn
我已使用本指南在 AWS EC2 实例上部署了 Django 应用程序。
一切正常,但当我下载 Excel 文件时出现错误。(Excel是使用opnenpyxl库创建的)。
下载xlsx文件时,应用程序正在返回download.htm文件。一切在开发服务器上都运行良好。
我试过
download标签。<a href="{% url 'download_estimate_excel_file' project.id project.name %}" class="dropdown-item" download> Download Excel </a>。[没用][service] Environment="LANG=ru_RU.UTF-8"到gunicorn.service文件(也在StackOverflow上找到了这个)。[没用]编辑:
Gunicorn 配置的路径:/etc/systemd/system/gunicorn.service
>> GUNICRON CONFIG
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myprojectdir
ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
myproject.wsgi:application
Environment="LANG=ru_RU.UTF-8"
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
Nginx 配置的路径:/etc/nginx/sites-available/<myproject>
>> NGINX CONFIG
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/sammy/myprojectdir;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Run Code Online (Sandbox Code Playgroud)
>> download_estimate_excel_file function
@login_required
def download_estimate_excel_file(request, project_id, project_name):
project_tnc_obj = ProjectTermsAndConditions.objects.filter(
project=project_id)
date_time_obj = datetime.now()
current_date = date_time_obj.strftime('%x')
current_time = date_time_obj.strftime('%X')
filename = project_name + '_' + str(current_date).replace('/', "-") + '_' + str(current_time).replace(":", "-") + ".xlsx"
file_path = "media/excel/" + filename
...
...
sheet.append([""])
workbook.save(filename=str(file_path))
workbook.close()
file_ecxel = FileResponse(open(file_path, 'rb'))
delete_file = os.remove(file_path)
return file_ecxel
Run Code Online (Sandbox Code Playgroud)
>> UPDATED download_estimate_excel_file function
@login_required
def download_estimate_excel_file(request, project_id, project_name):
project_tnc_obj = ProjectTermsAndConditions.objects.filter(
project=project_id)
date_time_obj = datetime.now()
current_date = date_time_obj.strftime('%x')
current_time = date_time_obj.strftime('%X')
filename = project_name + '_' + str(current_date).replace('/', "-") + '_' + str(current_time).replace(":", "-") + ".xlsx"
file_path = "media/excel/" + filename
...
...
sheet.append([""])
workbook.save(filename=str(file_path))
workbook.close()
with open(file_path, "rb") as excel:
data = excel.read()
delete_file = os.remove(file_path)
return HttpResponse(data, headers={
'Content-Type': 'application/vnd.ms-excel',
'Content-Disposition': 'attachment; filename= "{}"'.format(
filename),
})
Run Code Online (Sandbox Code Playgroud)
提前致谢。
| 归档时间: |
|
| 查看次数: |
414 次 |
| 最近记录: |