I have a django views function that converts csv into another delimiter format. But, I just want to convert csv file and reject other files.
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
I cannot use the below code because this works with only binary files, but the csv module wants to have text-mode files instead. Any alternatives to proceed with only csv files.
if not csv_file.name.endswith('.csv'):
messages.error(request, 'THIS IS NOT A CSV FILE')