为什么这个 django-python 片段也在上传的文档列表中显示已删除的文件?
视图.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)
)
列表.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>
浏览器输出
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?
我是 django-python 的新手,但我尝试调试它但发现了这个。
>>> 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>]
不知道怎么看实物??
谢谢
编辑: 正如答案和评论中指出的那样,我想告诉你我正在直接从上传的文件夹中删除文件。当文件夹中不存在文件时,有什么方法可以强制 ORM 对象自动更新