我有一個上傳的看法:驗證FileFiled URL在Django
views.py
def upload(request):
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(reverse('wiki.views.upload'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
return render_to_response(
'wiki/upload.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)
和我的模板 upload.html
{%extends "base.html"%}
{%block title%}Upload Documents{%endblock%}
{%block content%}
<!-- List of uploaded documents -->
{% if documents %}
<ul>
{% for document in documents %}
<a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a>
{% endfor %}
</ul>
{% else %}
<p>No documents.</p>
{% endif %}
<!-- Upload form. Note enctype attribute! -->
<form action="{% url upload %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.docfile.help_text }}
{{ form.docfile.label_tag }}
{{ form.docfile.errors }}
{{ form.docfile }}
<input type="submit" value="Upload" />
</form>
{%endblock%}
我上傳一些文件在指定的位置在{MEDIA_ROOT}之下並顯示在服務器上,但如果我從該文件夾中刪除該文件,該文件名仍顯示在網頁上。
如何驗證並使用我的模板顯示上傳的文件。
@ Goin可以ü請解釋通過將代碼放在我上面的代碼片段..感謝 – sunny 2012-01-30 16:49:55