简单的 Django 图像上传 - 图像文件不保存
是的,我正在学习如何制作一个简单的图像上传表单以将图像上传到 MEDIA_ROOT。表单呈现良好,我没有收到任何错误,但该文件未显示在 MEDIA_ROOT 目录中。如果遵循文档示例但无法使其工作,我知道这是因为我没有正确理解 django 文件上传处理。所以这是我的代码:
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 %}
希望这有足够的信息来获得一些帮助。请让我知道我还能提供什么。谢谢你,我真的很感谢你的帮助。
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}))
template file
{% 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要将其附加到模型,如 Matt S 所说,如果您的表单设置正确,request.FILES 包含图像的所有数据。
您需要确保在元素中使用
enctype="multipart/form-data"
,如文档所述: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/
您需要的一切都在那里有详细记录。希望这有帮助!
编辑OP请求有关文件上传的更多信息
来自文档:
他们有一个如何编写
handle_uploaded_file
函数的示例: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.
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-formAside 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 requested more information on File Uploads
From the docs:
And they have an example of how you could write the
handle_uploaded_file
function:为了上传您的文件并在 request.FILES 中显示,您的表单必须包含 enctype="multipart/form-data" ,如下所示:
In order to have your files uploaded and shows in request.FILES, your form MUST contain enctype="multipart/form-data" as shown below:
您需要将其附加到模型(作为 ImageField)因为它目前没有保存在任何地方。现在处理它可能没有任何问题,但在视图返回后它会被丢弃。没关系,我没有意识到模型是不必要的。
You need to attach it to a model (as an ImageField) as it's not being saved anywhere presently. It may be handled without any issues right now but it gets discarded after the view returns.Never mind, I didn't realize models were unnecessary.