I need to run a different code depending on the extension of the file.
This is a simplified version of the model:
JavaScript
x
3
1
class SuplierList(models.Model):
2
file = models.FileField(upload_to='fornecedores/')
3
This is the part of the view that I have the problem:
JavaScript
1
5
1
table = request.GET.get("table")
2
ft = FornecedorTabela.objects.filter(id=table)[0]
3
file = ft.file
4
if re.match('.pdf$', ft.file.name):
5
I don’t know how I should write this last condition above. With this version above, I get the error: The ‘python’ engine cannot iterate through this file buffer.
How show I write this condition?
Advertisement
Answer
Use str.endswith
Ex:
JavaScript
1
6
1
table = request.GET.get("table")
2
ft = FornecedorTabela.objects.filter(id=table)[0]
3
file = ft.file
4
if ft.file.name.endswith('.pdf'):
5
6