简单的Django图像上传 - 图像文件不保存 [英] Simple Django Image Upload - Image file not saving

查看:151
本文介绍了简单的Django图像上传 - 图像文件不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正确我正在学习如何做一个简单的图像上传表单将图像上传到MEDIA_ROOT。该表单呈现良好,我没有错误,但该文件没有显示在MEDIA_ROOT目录中。如果遵循文档示例,不能让它工作,我知道这是因为我没有理解django文件正确上传。所以这里是我的代码:

Right I'm learning how to do a simple image upload form to upload an image to MEDIA_ROOT. The form renders fine, I get no errors, but the file is not showing up in the MEDIA_ROOT directory. If followed the documentation example and can't get it to work and I know it is because I have not understood django file upload handeling properly. So here is my code:

forms.py

from django import forms

class UploadImageForm(forms.Form):
    image = forms.ImageField()

views.py

def merchant_image_upload(request):
    if request.method == 'POST':
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            FileUploadHandler(request.FILES['image'])
            return HttpResponseRedirect('/dashboard/gallery')
    else:
        form = UploadImageForm()
    return render_to_response('gallery.html', RequestContext(request, {'form': form}))

模板文件

{% extends 'base.html' %}
{% block main %}
    <form action="{% url scvd.views.merchant_image_upload %}" method="post">{% csrf_token %}
        {{ form.image }}
        <input type="submit" value="Upload" />
    </form>
{% endblock %}

希望这是足够的信息来获得帮助。请让我知道我还能提供什么谢谢,我非常感谢帮助。

Hopefully that's sufficient info to get some help. Please let me know what else I can provide. Thank you, I really appreciate the help.

推荐答案

你不需要把它附加到一个模型中,Matt S说,请求.FILES具有图像的所有数据,如果您的表单设置正确。

You don't need to attach it to a model as Matt S states, request.FILES has all the data for the image if your form is set up correctly.

您需要确保使用 enctype =multipart / form-data在您的元素中,文档状态为: https://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-files-to-a-form

You need to make sure use enctype="multipart/form-data" in your element, as the docs state here: https://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-files-to-a-form

除此之外,请阅读有关文件上传的文档: https://docs.djangoproject.com/en/dev/topics/http/file-uploads/

Aside from that, read the docs about file uploads: https://docs.djangoproject.com/en/dev/topics/http/file-uploads/

您需要的所有文件都记录在案。希望这有帮助!

All you need is well documented there. Hope this helps!

EDIT OP请求有关文件上传的更多信息

从文档:


大多数时候,您只需将请求中的文件数据传递到
中的表单,如将上传的文件绑定到表单。这将
看起来像:

Most of the time, you'll simply pass the file data from request into the form as described in Binding uploaded files to a form. This would look something like:



from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response

# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/success/url/')
    else:
        form = UploadFileForm()
    return render_to_response('upload.html', {'form': form})

他们有一个例子,说明如何编写 handle_uploaded_file 函数:

And they have an example of how you could write the handle_uploaded_file function:

def handle_uploaded_file(f):
    destination = open('some/file/name.txt', 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

这篇关于简单的Django图像上传 - 图像文件不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆