如何在请愿书中处理多个文件以恢复它们并在视图中显示它们?

时间:2019-04-26 12:26:22

标签: html django

我正在尝试使用django中的html格式上载多个图像文件,但是当触发按钮时,仅上载了一个文件,如何恢复多个文件并在视图中显示它们?

<form enctype="multipart/form-data" method="post">
    {% csrf_token %}
        <h4>Búsqueda general</h4>
        <input type="file" name="document" accept="image/*" multiple="multiple"> <br>
        <center><button type="submit" id="subir">Subir imagenes</button></center><br>
 </form>
 <ul>
        {% if uploaded_file_url %}
             <p>File uploaded at: <a href="{{ uploaded_file_url }}">{{ uploaded_file_url }}</a></p>
            <li><img src="{{ uploaded_file_url }}" title ="{{ filename }}" id="dragElement"></li>
        {% endif %}
</ul>

views.py

def cuad(request):
    if request.method == 'POST' and request.FILES.getlist('document', False):
        myfile = request.FILES['document']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)
        print(request.FILES)
        return render(request, "visores/cuadrantes.html", {
            'uploaded_file_url': uploaded_file_url,
            'filename': filename
        })
    return render(request, "visores/cuadrantes.html")

我在打印请求时在控制台上得到了这个

<MultiValueDict: {'document': [<TemporaryUploadedFile: P5140037.JPG 
(image/jpeg)>, <TemporaryUploadedFile: P5140038.JPG (image/jpeg)>, 
<TemporaryUploadedFile: P5140039.JPG (image/jpeg)>, 
<TemporaryUploadedFile: P5140043.JPG (image/jpeg)>]}>

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

def cuad(request):
    if request.method == 'POST' and request.FILES.getlist('document', False):
        myfiles = request.FILES['document']
        uploaded_documents = {}
        for document in myfiles:
            fs = FileSystemStorage()
            filename = fs.save(document.name, document)
            uploaded_file_url = (fs.url(filename))
            uploaded_documents[filename] = uploaded_file_url

        return render(request, "visores/cuadrantes.html", {
            'uploaded_documents': uploaded_documents,
        })
    return render(request, "visores/cuadrantes.html")

在模板中:

{% for document in uploaded_documents %}
    {% for filename, file_url in document.items %}
         <p>File uploaded at: <a href="{{ file_url }}">{{ file_url }}</a></p>
        <li><img src="{{ file_url }}" title ="{{ filename }}" id="dragElement"></li>
    {% endfor %}
{% endfor %}

答案 1 :(得分:0)

cuadrantes.html

<form enctype="multipart/form-data" method="post">
    {% csrf_token %}
        <h4>Búsqueda general</h4>
        <input type="file" name="document" accept="image/*" multiple="multiple"> <br>
        <center><button type="submit" id="subir">Subir imagenes</button></center><br>
</form>
{% for filename, uploaded_file_url in uploaded_documents.items %}
    <li><img src="{{ uploaded_file_url }}" title ="{{ filename }}" id="dragElement"></li>
{% endfor %}

views.py

@login_required
def cuad(request):
    uploaded_documents = {}
    if request.method == 'POST' and request.FILES.getlist('document', False):
        myfiles = request.FILES.getlist('document')
        for document in myfiles:
            fs = FileSystemStorage()
            filename = fs.save(document.name, document)
            uploaded_file_url = fs.url(filename)
            uploaded_documents[filename] = uploaded_file_url
    return render(request, "visores/cuadrantes.html", {
        'uploaded_documents': uploaded_documents,
    })