Uploading/Deleting 个来自 Django 数据库的文件

Uploading/Deleting files from a database in Django

我正在尝试找出一种方法来删除我上传到 django 轮播中的图片。有人对此有解决方案吗?我找不到任何对我来说很清楚的在线示例。您可以假设我已经导入了所有内容,并且所有模型等都是正确的。

我的代码如下:

carousel.html:

 {% load staticfiles %}
{% load filename %}

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div id="myCarousel" class="carousel slide" data-ride="carousel">


                <div class="carousel-inner" role="listbox">
                    {% for document in documents %}
 <div class="item {% if forloop.first %} active {% endif %}"> 
    <div class="row">
      <div class="col">
        <li><a href = "{{document.docfile.url}}">{{document.docfile.name}}</a></li>
        <img src = "{{STATIC_URL}}img/{{document|filename}}" >
<p align="center"><form style="text-align:center" action="{% url 'webportal:delete' %}" method="post" enctype="multipart/form-data">
            {% csrf_token %}
<p>{{ form.non_field_errors }}</p>
            <p>{{ form.Document.label_tag }} {{ form.docfile.help_text }}</p>
            <p>
                {{ form.Document.errors }}
                {{ form.Document }}
            </p> 
            <p><input type="submit" value="Delete" /></p>
        </form></p>
      </div>
    </div>
  </div>
  {% endfor %}
                </div>
                <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
            <!-- /.carousel -->
        </div>
    </div>
<form action="{% url 'webportal:carousel' %}" 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>

</div>

Views.py

def delete(request, my_id):
    Deleted=get_object_or_404(Document, docfile=my_id)
    if request.method=='POST':
        form=DeleteForm(request.POST, instance=Deleted)
        if form.is_valid():
            Deleted.delete()
            return HttpResponseRedirect('http://127.0.0.1:8000/alzheimers/')
    else:
        form=DeleteForm(instance=Deleted)
    return render_to_response(
        'webportal/index.html',
        {'documents': documents, 'form': form,},
        context_instance=RequestContext(request)
    )        

            # Redirect to the document list after POST
def carousel(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect('http://127.0.0.1:8000/alzheimers/')

    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()
    #documents=DocumentForm().
    # Render list page with the documents and the form
    return render_to_response(
        'webportal/index.html',
        {'documents': documents, 'form': form,},
        context_instance=RequestContext(request)
    )

你不想在一个视图中处理太多。你可以,但它很难维护代码。最好添加一个单独的视图以进行删除。在你的模板循环中使用这个。

<a href='{% url 'delete_document' pk=document.pk %}'>delete</a> 

然后添加一个带有 pk 参数的新 url 模式:

url(r'^document/delete/(?P<pk>\d+)/$', "delete_document", name="delete_document"),

还有这样的视图:

def delete_document(request, pk):
    try:
        Docuent.objects.get(pk=pk).delete()
    except Document.DoesNotExist:
        pass
    return HttpResponseRedirect(...)