0

I am trying to develop a Django app able to CRUD files. At the moment I have developed the Upload-Download functionalities but things get pretty hard for me when I want to delete items.

* Views.py *

def list(request):
# Handle file upload
if request.method == 'POST':
    form = DocumentForm(request.POST, request.FILES)
    all_docs = Document.objects.all()
    repeated = False
    if form.is_valid():
        for doc in all_docs:
            if Document(docfile=request.FILES['docfile']).docfile == doc.docfile:
                repeated = True
        if not repeated:
            newdoc = Document(docfile=request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('list'))
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(
    request,
    'list.html',
    {'documents': documents, 'form': form}
)

* Forms.py *

class DocumentForm(forms.Form):
docfile = forms.FileField(
    label='Select a file'
)

* List.html *

<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 "list" %}" 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" value="Upload"/></p>
    </form>
</body>

* Output *

enter image description here

As you can see when I Upload a document it gets stored in the DB, but I dont know how to make a button to be able to select an Item and delete it, as I have done with the uploading system.

The model of Document has an atribute ID and a FileField.

I'm aware that this is a really concrete and dense question but I have spent too many hours doing research about it and I am not getting results. I started to develop this piece of code to make a view able to delete the item, but I dont know how to select it:

* Delete View *

def remove_document(request):

if request.method == 'POST':
    form = DocumentForm()
    # Dont know how to reference the item I want to delete
    document = request.POST.objects.get(docfile = ?????)

    db_documents = Document.objects.all()

    for db_doc in db_documents:
        if db_doc = document:
            document.delete()

return render(
        request,
        'list.html',
        {'documents': documents, 'form': form}
    )

I am pretty new at Django and its architecture model gets so tricky to me

Thanks!

Across
  • 63
  • 5
  • 1
    Unfortunately this is really too broad for a proper answer. Your update code is quite confused, which is leading you astray for writing your delete code. As a hint, you should not be trying to iterate over all documents in either of those methods; you should get the instance corresponding to the ID you receive directly via `.objects.get()`, and then either update or delete it. – Daniel Roseman Apr 18 '18 at 17:16
  • I guess I'll put it in a different way, a more "common" way. Thanks for the answer! – Across Apr 19 '18 at 14:48

0 Answers0