How to delete File Field and File(in project root) in django?
I want to delete the data inside the file field and the file uploaded through the file field.
class Fruits(Signable): name = models.CharField( _('Name'), max_length=200, ) description = models.TextField( _('Description'), null=True, blank=True, ) this_is_my_file = models.FileField( _('Photo image'), null=True, blank=True, upload_to='myfile/' ) def __str__(self): return self.name def delete(self, *args, **kwargs): if self.this_is_my_file: self.this_is_my_file.delete() super().delete(*args, **kwargs)
Like the code above, I added 'def delete' to the existing model as shown above through search.
But I don't know how to use this function.
How do I use this in view and template?
My part of view is below.
class FruitsDetailView(LoginRequiredMixin, DetailView): template_name = 'frutis/detail.html' login_url = 'login' model = MyObjects def get_context_data(self, **kwargs): pk = self.object.pk context = super().get_context_data(**kwargs) ... return context
How do I execute the delete function in this view?
I want to delete it when I press the 'delete' button on the template.
Should I send a post request from template and receive it from view?
I've been thinking about it for 4 days, but I don't know what to do.