从Django中的数据库上传/删除文件

时间:2015-03-22 11:06:04

标签: python html django upload

我试图想办法删除我已经上传到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)
    )

1 个答案:

答案 0 :(得分:1)

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

<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(...)