python - Django 中的图像字段是否需要额外的代码

标签 python django imagefield

我在模型中有这个

image_name = models.ImageField(upload_to='accounts/')

在我看来我有

def account_form(request):
    if request.method == 'POST': # If the form has been submitted...
        form = AccountForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
                form.save()
                return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = AccountForm() # An unbound form

    return render_to_response('account_form.html', {
            'form': form,
    })

我需要做额外的编码来保存图像还是django会自己做

最佳答案

还要确保在 HTML 中设置了表单 enctype 以提交文件数据:

<form action="..." method="POST" enctype="multipart/form-data">

关于python - Django 中的图像字段是否需要额外的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6489173/

相关文章:

python - 在Python中根据计数和唯一计数添加列

python - simplekml 中的 html 弹出窗口

python - 多对多关系 禁止直接分配到多对多集合的前向端

python - 如何在 django GET api(rest 框架)中返回单个对象

python - 无效的 block 标记应为 'empty' 或 'endfor'。尝试显示 ImageField 时出错

python - 通过 Django Admin 将图像上传到 ImageField 时出错

python - 链接 loc 和 iloc 后更改 Pandas 中的值

Python 生成器与列表作为数组初始值设定项

django - 如何切换到新数据库

python - Django图像字段: full or relative path?