Udit Gupta
Udit Gupta

Reputation: 3272

django python document object listing even deleted files

Why this django-python snippet showing deleted files also in uploaded document list ??

views.py

def lists(request):
    # Handle file upload
    if request.method == 'POST':
            form = DocumentForm(request.POST, request.FILES)
            if form.is_valid():
                    filename = Document(docfile = request.FILES['docfile'])
                    filename.save()
            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('sdm:lists'))
            #  return render_to_response(reverse('sdm:lists'))
    else:
            form = DocumentForm() # A empty, unbound form

# Load documents for the list page
    documents = Document.objects.all()

# Render list page with the documents and the form
    return render_to_response(
            'sdm/lists.html',
            {'documents': documents, 'form': form},
            context_instance=RequestContext(request)
    )

lists.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Minimal Django File Upload Example</title>   
</head>
<body>
<!-- List of uploaded documents -->
{% if documents %}
    <ul>
    {% for document in documents %}
        <li><a href="{{document.docfile.url }}">{{ document.docfile.name }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No documents.</p>
{% endif %}

    <!-- Upload form. Note enctype attribute! -->
    <form action="{% url sdm:lists %}" method="post" enctype="multipart/form-data">
           {% csrf_token %}
      <p>{{form.non_field_errors }}</p>
        <p>{{form.docfile.label_tag }} {{form.docfile.help_text }}</p>
        <p>
            {{ form.docfile.errors }}
            {{ form.docfile }}
        </p>
        <p><input type="submit" name="press" value="Upload" /></p>
    </form>
</body>

Browser Output

f14 is latest uploaded file and rest all files have been deleted from respective folders but why is that it is still showing all files in list when are not even present?

I am new to django-python but I tried to debug it but found this.

>>> from sdm.models import Document
>>> Document.objects.all()
 [<Document: Document object>, <Document: Document object>, <Document: Document    
 object>,<Document: Document object>, <Document: Document object>, <Document: Document 
 object>,<Document: Document object>, <Document: Document object>, <Document: Document 
 object>,<Document: Document object>, <Document: Document object>, <Document: Document 
 object>,<Document: Document object>, <Document: Document object>, <Document: Document 
 object>]

Don't know how to see actual objects ??

enter image description here

Thanks

Edit : As pointed out in answers and comments, I would like to tell you that I am deleting files from the uploaded folder directly. Is there any way that I can force ORM object to update automatically when files are not present in folder

Upvotes: 0

Views: 552

Answers (1)

scriptmonster
scriptmonster

Reputation: 2771

I couldn't see how you delete a Document. You should have a method like the following.

def delete(request):
    if request.method != 'POST':
        raise HTTP404 # don't forget to import
    else:
        docId = request.POST.get('doc-id', None)

    if docId is not None:
        docToDel = Document.objects.get(pk=docId)

        # delete the file using docToDel.docfile

        docToDel.delete()

    return HttpResponse('Whatever message you want.')

UPDATE 1

If you would like to delete objects automatically when a file deleted directly, you could iterate the Document objects and check if the files are existing or not. But you need to run this procedure before listing or periodically.

Upvotes: 1

Related Questions