无法使用图像验证django表单

时间:2011-02-22 18:28:10

标签: django django-forms django-views django-models

我在制作表单时遇到了麻烦。在我看来,一切都很好,但is_valid()总是返回False(我必须在shell模式下检查它,因为在模板中它没有显示任何错误)。我错过了什么吗?

如果有人想测试它,可以从http://gitorious.org/e-cidadania

下载

forms.py

from django.forms import ModelForm
from e_cidadania.apps.spaces.models import Space

class SpaceForm(ModelForm):
    class Meta:
        model = Space

views.py

@permission_required('Space.add_space')
def create_space(request):

space = Space()
if request.POST:
    form = SpaceForm(request.POST, request.FILES, instance=space)
    if form.is_valid():
        handle_uploaded_file(request.FILES['file'])
        form.author = request.user
        form.date = datetime.datetime.now()
        form.save()
        return render_to_response('/')
else:
    form = SpaceForm()
return render_to_response('spaces/add.html',
                         {'form': form},
                         context_instance=RequestContext(request)) 

models.py

class Space(models.Model):

    name = models.CharField(_('Name'), max_length=100, unique=True,
                            help_text=_('All lowercase. Obligatory.'))
    description = models.TextField(_('Description'))
    date = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(User, verbose_name=_('Author'))
    logo = models.ImageField(upload_to='spaces/logos',
                             verbose_name=_('Logotype'),
                             help_text=_('100px width, 75px height'))
    banner = models.ImageField(upload_to='spaces/banners',
                               verbose_name=_('Banner'),
                               help_text=_('75px height'))
    authorized_groups = models.ManyToManyField(Group,
                                               verbose_name=_('Authorized groups'))

    mod_debate = models.BooleanField(_('Debate module'))
    mod_proposals = models.BooleanField(_('Proposals module'))
    mod_news = models.BooleanField(_('News module'))
    mod_cal = models.BooleanField(_('Calendar module'))
    mod_docs = models.BooleanField(_('Documents module'))

4 个答案:

答案 0 :(得分:1)

form.errors显示没有错误?

涉及文件时,请检查request.FILES是否确实有文件。

确保您的<form><form enctype="multipart/form-data" ...> ..在许多情况下,这是罪魁祸首。

该错误的所有Google搜索结果都围绕着PIL。特别是如果你在Mac上!

http://salamand.wordpress.com/2009/08/25/problem-uploading-image-file-to-satchmo/ http://djangodays.com/2008/09/03/django-imagefield-validation-error-caused-by-incorrect-pil-installation-on-mac/
http://mail.python.org/pipermail/image-sig/2002-August/001947.html

答案 1 :(得分:1)

问题是模型字段author和date未声明为blank = True,null = True。因为表单从未经过验证,因为即使您不提交save(),save命令也会验证表单。

答案 2 :(得分:0)

Yuji可能回答了你的问题,但我想提一下如何使视图更清晰(相同的含义,更少的代码和可读性相同甚至更好):

@permission_required('Space.add_space')
def create_space(request):

    space = Space()
    form = SpaceForm(request.POST or None, request.FILES or None, instance=space)

    if request.POST and form.is_valid():
        handle_uploaded_file(request.FILES['file'])
        form.author = request.user
        form.date = datetime.datetime.now()
        form.save()
        return render_to_response('/')

    return render_to_response('spaces/add.html',
                             {'form': form},
                             context_instance=RequestContext(request)) 

答案 3 :(得分:0)

我不知道它是否会有所帮助,但是一旦我检查了用户权限时出现了一些错误,那就提交表单了,这就是诀窍:

   if form.is_valid():
        new_space = form.save(commit = False)
        new_space.author = request.user
        ...
        new_space.save()