开发者

Cannot validate a django form with images

开发者 https://www.devze.com 2023-02-12 11:23 出处:网络
I\'m having a trouble making a form work. As I see it, everything is fine, but is_valid() always returns False (I had to check it in shell mode, since in the template it doesn\'t show any errors). Am

I'm having a trouble making a form work. As I see it, everything is fine, but is_valid() always returns False (I had to check it in shell mode, since in the template it doesn't show any errors). Am I missing something?

If someone wants to test it, it can be downloaded from 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 = Spa开发者_StackOverflow社区ceForm(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'))


form.errors shows no errors?

When files are involved, check if request.FILES actually has a file.

Ensure your <form> has <form enctype="multipart/form-data" ...> .. this is the culprit in many cases.

All the google results for that error revolve around PIL. Especially if you're on a 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


The problem was that the model fields author and date were not declared as blank=True, null=True. Because of that the form never validated, because even if you don't commit the save(), the save command does validate the form.


Yuji probably answered your question but I would like to give a tip on how to make view cleaner (same meaning, a bit less code and readability same or even better):

@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)) 


I don't know if it will help, but once i got some errors when I checked permissions for user, that submit the form, and this makes the trick:

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

精彩评论

暂无评论...
验证码 换一张
取 消