简体   繁体   English

图片上传未在Django中保存文件

[英]Image upload not saving file in Django

No errors when hitting upload. 点击上传时没有错误。 But the image doesn't appear where I have indicated it ought to. 但是图像没有出现在我已指示的位置。 Put an absolute path in MEDIA_ROOT and referenced the same in (upload_to) param ImageField. 将绝对路径放在MEDIA_ROOT中,并在(upload_to)参数ImageField中引用相同的路径。 Not sure what I am missing. 不知道我在想什么。

Model: 模型:

class FileUploadHandler(models.Model):
    title = models.CharField(max_length=100)
    file = models.ImageField(upload_to='/Python27/Lib/site-packages/django/bin/mideastinfo/wiki/static/')

View: 视图:

from models import Article, Edit
from forms import ArticleForm, EditForm
from forms import *
from PIL import Image
from models import FileUploadHandler

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

Forms.py: Forms.py:

class UploadImageForm(forms.ModelForm):
    class Meta:
    model = FileUploadHandler
    #image = forms.ImageField()\
    fields = ['title']

Here you didn't save it in views. 在这里,您没有将其保存在视图中。 Just use this: 只需使用此:

 if request.method == 'POST':
    form = UploadImageForm(request.POST, request.FILES)
    if form.is_valid():
        newfile = FileUploadHandler(title='anything', file=request.FILES['image'])
        newfile.save()

I think you're working a little too hard. 我认为您的工作有些辛苦。 You shouldn't have to call the FileUploadHandler model directly to save the image. 您不必直接调用FileUploadHandler模​​型来保存图像。

First, your form should be: 首先,您的表格应为:

class UploadImageForm(forms.ModelForm):
    class Meta:
        model = FileUploadHandler

And your view should be: 您的看法应该是:

from forms import UploadImageForm

def image_upload(request):
    form = UploadImageForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        form.save()
    return render_to_response('wiki/gallery.html', RequestContext(request, {'form': form}))

Since you're using a ModelForm, you just need to save the form once it's been validated. 由于您使用的是ModelForm,因此只需要在验证表单后保存即可。 Just make sure that the file field is visible in your template. 只需确保文件字段在模板中可见。 You were excluding the 'file' field in your form earlier that's why it wasn't working. 您之前在表单中排除了“文件”字段,这就是为什么它不起作用的原因。

Update: Simplified your view even more just because... 更新:仅仅因为...而简化了您的视图

The solution might be the template: 解决方案可能是模板:

<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form }}
    <input type="submit" name="submit" value="Submit" />
</form>

Notice: enctype="multipart/form-data" 注意: enctype="multipart/form-data"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM