【发布时间】:2021-03-21 20:02:46
【问题描述】:
我有一个 django 视图函数,可以将 csv 转换为另一种分隔符格式。但是,我只想转换 csv 文件并拒绝其他文件。
def index(request):
csv_form = ''
if request.method == 'POST':
csv_form = CsvForm(request.POST, request.FILES)
if csv_form.is_valid():
csv_file = TextIOWrapper(request.FILES['csv_file'].file, encoding='ascii', errors='replace')
#other actions
我不能使用下面的代码,因为它只适用于二进制文件,但 csv 模块希望使用文本模式文件。仅处理 csv 文件的任何替代方法。
if not csv_file.name.endswith('.csv'):
messages.error(request, 'THIS IS NOT A CSV FILE')
【问题讨论】:
标签: django csv django-views django-forms file-handling