本文介绍了Django Imagefield通过ModelForm无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信我正在做一些很明显愚蠢的事情,但是我一直在努力想出几个小时,没有什么是跳出来的。



我使用的是一个ModelForm,所以我可以从模型中公开几个字段进行编辑。 2x ImageField,1x TextField。表单被处理并且TextField工作。两个ImageFields不起作用,这就是为什么我今天在这里。



我正在使用Django 1.0.2



以下是相关代码(询问是否需要更多信息 - 而且我不包括HTML,因为该部分似乎正常工作):



模型:

  class公司(models.Model):
#...
logo = models.ImageField (upload_to ='logos',blank = True)
intro_pic = models.ImageField(upload_to ='intropics',blank = True)
intro_text = models.TextField(blank = True)

查看和表单:

  def admin_edit(request,company_slug):
company = get_object_or_404(Company,slug = company_slug)

f = AdminEditForm(instance = company)
if request.method ==' POST':
f = AdminEditForm(request.POST,instance = company)
如果f.is_valid():
打印处理表单
print f.cleaned_data [ intro_pic']
f.save()

return render_to_response('uadmin / edit.html',{'company':company,'f':f},RequestContext(request)


class AdminEditForm(ModelForm):
class Meta:
model = Company
fields = ['logo','intro_pic','intro_text' ]


解决方案

好吧,我觉得像个白痴。为了使Django能够处理上传的文件,您需要将request.FILES变量传递给表单(有意义,对吗?)



在我的情况下以下行来自:

  f = AdminEditForm(request.POST,instance = company)

To:

  f = AdminEditForm (request.POST,request.FILES,instance = company)

另一件事要检查(如果你运行将来这样的东西就是你的形式是多部分的。您的< form> 标签应如下所示:

  ; form enctype =multipart / form-datamethod =postaction => 


I'm certain I'm doing something really obviously stupid, but I've been trying to figure it out for a few hours now and nothing is jumping out at me.

I'm using a ModelForm so I can expose a few fields from a model for editing. 2x ImageField, 1x TextField. The Form is processed and the TextField works. The two ImageFields do not work and they're why I'm here today.

I'm using Django 1.0.2

Here's the relevant code (ask if you need more -- and I'm not including the HTML because that part appears to work fine):

Model:

class Company(models.Model):
    #...
    logo = models.ImageField(upload_to='logos', blank=True)
    intro_pic = models.ImageField(upload_to='intropics', blank=True)
    intro_text = models.TextField(blank=True)

View and form:

def admin_edit(request, company_slug):
    company = get_object_or_404(Company, slug = company_slug)

    f = AdminEditForm(instance = company)
    if request.method == 'POST':
        f = AdminEditForm(request.POST, instance = company)
        if f.is_valid():
            print "Processing form"
            print f.cleaned_data['intro_pic']
            f.save()

    return render_to_response('uadmin/edit.html', {'company':company, 'f':f}, RequestContext(request))


class AdminEditForm(ModelForm):
    class Meta:
        model = Company
        fields = ['logo', 'intro_pic', 'intro_text']
解决方案

Well I feel like an idiot. In order for Django to be able to process uploaded files, you need to pass the request.FILES variable to the form (makes sense, right?!)

In my case the following line goes from:

f = AdminEditForm(request.POST, instance = company)

To:

f = AdminEditForm(request.POST, request.FILES, instance = company)

Another thing to check (if you run into something like this in the future) is that your form is multipart. Your <form> tag should look something like this:

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

这篇关于Django Imagefield通过ModelForm无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:54