1

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/

https://docs.djangoproject.com/en/2.2/ref/class-based-views/generic-editing/#django.views.generic.edit.DeleteView

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

1 Answers1

1

the request.POST is a QueryDict of key/value pairs of the corresponding name/value of your form inputs.

I think the problem is that you forgot to add a name to your pk input, so when your view gets the request.POST object, there isn't a key called pk.

.getlist('arg') will return an empty list if no keys called arg are found, and no default value is given, which results in the error you get.

This should fix your problem:

<input type="hidden" name="pk" value="{{ pk }}">

Update

I believe the error you are gettting is unrelated to your original problem, and probably should be asked as a new question. That being said, I will still try to help, I just have no experience using DRF.

According to the docs:

.accepted_renderer

The renderer instance that will be used to render the response. Set automatically by the APIView or @api_view immediately before the response is returned from the view.

Also:

Unless you want to heavily customize REST framework for some reason, you should always use an APIView class or @api_view function for views that return Response objects. Doing so ensures that the view can perform content negotiation and select the appropriate renderer for the response, before it is returned from the view.

I think your best option would be to use the @api_view decorator (although I'm not sure if this works in classed based views).

from rest_framework.decorators import api_view
from rest_framework.response import Response

class FileDeleteView(DeleteView):

    model = FileModel
    fields = ['file']
    template_name = 'delete_success.html'
    success_url = '/delete_success/'

    @api_view
    def post(self, request):
    ...
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80