简体   繁体   English

Django - 保存上传的图像

[英]Django - Save an uploaded image

I have a form where I am supposed to upload an image, but I can't get the image to save. 我有一个表格,我应该上传图像,但我无法保存图像。 Everything else in the form works properly except the image. 除图像外,表单中的其他所有内容都能正常工作。

I'm sure there are some issues with my addGame method, but I've tried it dozens of different ways with no luck. 我确定我的addGame方法存在一些问题,但我已经尝试了几十种不同的方法而没有运气。

I've gone through the documentation , but it appears I'm still doing something wrong, as the image never gets saved. 我已经阅读了文档 ,但看起来我仍然做错了,因为图像永远不会被保存。

(Just as a side note: I'm using Pillow for cropping the image, and I'm not sure if I'm doing that properly either, but I just added that in recently, and since the image doesn't save I have no way of knowing whether that is implemented correctly. I'm leaving the cropping part commented out while I try to get the upload to work.) (正如旁注:我正在使用枕头来裁剪图像,而且我不确定我是否也正确地做到了这一点,但我最近刚补充说,因为图像没有保存我有无法知道这是否正确实现。当我尝试上传工作时,我将离开裁剪部分注释掉。)

forms.py

class GameForm(forms.ModelForm):

    image = forms.ImageField()
    code = forms.Textarea()
    deleteGame = forms.BooleanField(required=False, widget=forms.HiddenInput())

    class Meta:
        model = Game
        fields = ('title', 'image', 'description', 'requirements', 'code', 'deleteGame')

views.py : views.py

@login_required
def add_game(request):
    user = request.user

    if request.method == 'POST':
        form = GameForm(request.POST, request.FILES)
        if form.is_valid():
            form = form.save(commit=False)
            image = request.FILES['image']
            box = (200, 200, 200, 200)
            cropped = image.crop(box)
            form.image = cropped
            form.user = request.user
            form.save()
            return HttpResponseRedirect('/userprofile')
    else:
        form = GameForm()

    args = {}
    args.update(csrf(request))
    args['user'] = user
    args['form'] = form

    return render_to_response('addgame.html', args)

models.py

class Game(models.Model):
    user = models.ForeignKey(User, blank=True)
    title = models.CharField(max_length=256)
    image = models.ImageField(upload_to='games', blank=True)
    description = models.CharField(max_length=256)
    requirements = models.CharField(max_length=256)
    code = models.TextField()
    deleteGame = models.BooleanField(default=False)

    def __unicode__(self):
        return self.title

My media settings look like this: 我的媒体设置如下所示:

MEDIA_ROOT = 'media/'
MEDIA_URL = '/media/'

File Structure: 文件结构:

If I add an image via the admin portal, it saves properly, but I get an error like this in my log: 如果我通过管理门户添加图像,它会正确保存,但我在日志中收到如下错误:

Not Found: /media/games/Screen_Shot_2015-12-29_at_1.03.05_AM.png 找不到:/media/games/Screen_Shot_2015-12-29_at_1.03.05_AM.png

如果您打算将文件与表单数据一起发送,则模板中的form标记需要enctype="multipart/form-data"

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

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