I am trying to delete an Object from the database of my Django app.
The idea is to get the primary from the html file and post it in a form. I am not understanding how I should pass the primary key to post my delete request in the correct way.
I went to everything I found about class based views DeleteView both on stackoverflow, django doc and other sources without figuring out how this works.
E.g.: Django delete FileField
How to delete a record in Django models?
Python Django delete current object
How to delete a record in Django models?
https://docs.djangoproject.com/en/2.2/topics/db/queries/
Below the main snippets of the code, if you are missing something needed just let me know.
views.py
class SelectFileDelView(TemplateView):
"""
This view is used to select a file from the list of files in the server.
After the selection, it will send the file to the server.
The server will then delete the file.
"""
template_name = 'select_file_deletion.html'
parser_classes = FormParser
queryset = FileModel.objects.all()
def get_context_data(self, **kwargs):
"""
This function is used to render the list of files in the MEDIA_ROOT in the html template
and to get the pk (primary key) of each file.
"""
context = super().get_context_data(**kwargs)
media_path = settings.MEDIA_ROOT
myfiles = [f for f in listdir(media_path) if isfile(join(media_path, f))]
pk_list = []
for value in myfiles:
pk = FileModel.objects.filter(file=value).values_list('pk', flat=True)
pk_list.append(pk)
file_and_pk = zip(myfiles, pk_list)
context['filename'] = file_and_pk
return context
class FileDeleteView(DeleteView):
"""
This class contains the method to delete a file interacting directly with the API.
DELETE requests are accepted.
"""
model = FileModel
fields = ['file']
template_name = 'delete_success.html'
success_url = '/delete_success/'
def post(self, request):
"""
This method is used to making predictions on audio files
loaded with FileView.post
"""
pk = request.POST.getlist('pk').pop()
try:
return Response(pk, status=status.HTTP_200_OK)
except ValueError as err:
return Response(str(err), status=status.HTTP_400_BAD_REQUEST)
select_file_deletion.html
{% extends "index.html" %}
{% block content %}
<form action="/App/delete/" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<h5>Please select one file at a time from the list below to delete it from the server.</h5>
{% for myfile, pk in filename %}
<p>Are you sure you want to delete "{{ myfile }}"?</p>
<input type="submit" value="Confirm">
<input type="hidden" name="pk" value="{{ pk }}">
<br>
{% endfor %}
<br>
</form>
{% endblock %}
urls.py
urlpatterns = [
# Url to select a file to be deleted and confirm the upload
url('filedelete/', SelectFileDelView.as_view(), name='file_delete'),
url('delete_success/(?P<pk>\d+)/$', FileDeleteView.as_view(), name='delete_success'),
]
models.py
"""
Models.py includes the database structure of the application.
"""
from django.db import models
from django.conf import settings
from django.dispatch import receiver
from django.db.models.signals import post_delete
class FileModel(models.Model):
file = models.FileField(null=True, blank=True)
timestamp = models.DateTimeField(auto_now_add=True)
path = models.FilePathField(path=settings.MEDIA_ROOT, default=settings.MEDIA_ROOT)
@receiver(post_delete, sender=FileModel)
def submission_delete(sender, instance, **kwargs):
"""
This function is used to delete attachments when a file object is deleted.
Django does not do this automatically.
"""
instance.file.delete(False)
ERROR:
IndexError at /App/delete/
pop from empty list
Request Method: POST
Request URL: http://127.0.0.1:8000/App/delete/
Django Version: 2.2.4
Exception Type: IndexError
Exception Value:
pop from empty list
Exception Location: /Users/marcogdepinto/PycharmProjects/DjangoRestDeepLearning/App/views.py in post, line 131
Python Executable: /Users/marcogdepinto/anaconda3/bin/python
Python Version: 3.6.9
UPDATE1: the html has been changed as below
<input type="hidden" name="pk" value="{{ pk }}">
Doing this, I am getting an
AssertionError at /App/delete/
.accepted_renderer not set on Response
Full traceback below:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/App/delete/
Django Version: 2.2.4
Python Version: 3.6.9
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'App',
'debug_toolbar']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Traceback:
File "/Users/marcogdepinto/anaconda3/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/Users/marcogdepinto/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
145. response = self.process_exception_by_middleware(e, request)
File "/Users/marcogdepinto/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
143. response = response.render()
File "/Users/marcogdepinto/anaconda3/lib/python3.6/site-packages/django/template/response.py" in render
106. self.content = self.rendered_content
File "/Users/marcogdepinto/anaconda3/lib/python3.6/site-packages/rest_framework/response.py" in rendered_content
55. assert renderer, ".accepted_renderer not set on Response"
Exception Type: AssertionError at /App/delete/
Exception Value: .accepted_renderer not set on Response