python - 如何在 django 的媒体文件夹中创建和保存 csv 文件?
问题描述
我在 django 中使用 HttpResponse 创建一个 csv 文件,我还想将它保存在媒体文件夹中。我在 settings.py 中设置了 MEDIA_ROOT
def to_csv(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=industry.csv'
writer = csv.writer(response)
data = Industry.objects.all()
writer.writerow(['Name','Workplace and Designation','Contact No.','Email ID','Purpose of Visit','Self Employed Details'])
for row in data:
writer.writerow([row.name, row.wp_des, row.mobileno, row.email, row.purpose, row.semp_details])
return response
它创建并下载 csv 文件,但我也想将它保存在媒体文件夹中
解决方案
使用settings
:
from django.conf import settings
writer = csv.writer(settings.MEDIA_ROOT / file)
推荐阅读
- javascript - HTML5 画布颜色和 JS
- javascript - Javascript重命名时赋值表达式的左侧无效
- java - Exoplayer 的 Firebase 存储 402 错误
- android - 构建工具和 sdk 更新后,带有 viewpager 和 FragmentPagerAdapter 的 Tablayout 在垂直滚动时崩溃
- java - 如何在代码中实现聚合和关联?
- ionic-framework - 创建 ionic 项目时,运行 subprocess npm 时出现错误
- android - 具有多个 URL 的多个按钮的 Webview - 空指针异常
- amazon-web-services - 如何使用 Amazon Lex 在一个话语中添加多个相同类型的插槽?
- sql - 如何在 Stack Exchange 数据资源管理器中获取所有用户的帖子标签(包括答案标签)?
- google-cloud-platform - 是否可以在同一命令中使用多个服务密钥