Here I successfully uploaded files in database(working fine). The issue here is file is stored in the database and if in case the media folder gets deleted it will raise an error [WinError 2] The system cannot find the file specified: ... So my question is how can I handle this error.
If the file is in database but not in the media folder then I want to handle this error and display the template without error. How can I do this ?
template
{% for document in documents %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{document.filename|truncatechars:15}}</td>
<td>{{document.file.size|filesizeformat}}</td>
<td>{{document.category}}</td>
<td>{{document.file.url}}</td>
<tr>
{% endfor %}
models
class Document(models.Model):
category = models.ForeignKey(DocumentCategory, on_delete=models.CASCADE)
file = models.FileField(upload_to='media/',
validators=[FileExtensionValidator(['pdf', 'xlsx', 'pptx', 'docx','xls']), file_size])
created = models.DateTimeField(auto_now_add=True)
@property
def filename(self):
return os.path.basename(self.file.name)