tags:

views:

437

answers:

3

Getting a rendering error for this form:

'NoneType' object has no attribute 'widget' http://dpaste.com/88585/

Any ideas?

view

def your_photos_upload(request):
    if request.method == 'POST':
        form = MemorialUserPhotosForm(request.FILES)
        if form.is_valid():

form

class MemorialUserPhotosForm(ModelForm):
    image = forms.ImageField(label="Select an image to upload.",  error_messages={'missing' : 'No image selected',  'invalid' : 'Please select an image',  'invalid_image' : 'Not a valid image file'})

    class Meta:
        model=MemorialUserPhotos

        fields=('image')

html

<form enctype="multipart/form-data" method="post" action="{{ processor }}">
<li><label for="id_image">Select an image to upload.</label> <input type="file" id="id_image" name="image"/></li>
<input type="hidden" name="sessionid" value="7e4b4626c6ae1021ba0bae7969bfdd14">
<input type="submit">
</form>

models

class MemorialUserPhotos(Photo):
    user = models.ForeignKey(User)
    modified = models.DateTimeField(auto_now=True)
    class Meta:
        db_table = u'memorial_user_photos'
        verbose_name = 'User Uploaded Photo'
        verbose_name_plural = 'User Uploaded Photo'
        ordering = ['user']
        get_latest_by = 'modified'

    def save(self):
        self.is_public = False
        if self.title is None:
            self.image.name
        if self.title_slug is None:
            self.title_slug = slugify(self.title)
        super(MemorialUserPhotos, self).save()
        gallery = Gallery.objects.get(id=settings.USER_GALLERY)
        gallery.photos.add(self)


class Photo(ImageModel):
    title = models.CharField(_('title'), max_length=100, unique=True)
    title_slug = models.SlugField(_('slug'), unique=True,
                                  help_text=('A "slug" is a unique URL-friendly title for an object.'))
    caption = models.TextField(_('caption'), blank=True)
    date_added = models.DateTimeField(_('date added'), default=datetime.now, editable=False)
    is_public = models.BooleanField(_('is public'), default=True, help_text=_('Public photographs will be displayed in the default views.'))
    tags = TagField(help_text=tagfield_help_text, verbose_name=_('tags'))



class ImageModel(models.Model):
    image = models.ImageField(_('image'), max_length=IMAGE_FIELD_MAX_LENGTH, 
                              upload_to=get_storage_path)
    date_taken = models.DateTimeField(_('date taken'), null=True, blank=True, editable=False)
    view_count = models.PositiveIntegerField(default=0, editable=False)
    crop_from = models.CharField(_('crop from'), blank=True, max_length=10, default='center', choices=CROP_ANCHOR_CHOICES)
    effect = models.ForeignKey('PhotoEffect', null=True, blank=True, related_name="%(class)s_related", verbose_name=_('effect'))
A: 

Seems that Django doesn't like having only a file field to check against, adding a CharField to the form made it much happier right away

Alvin
+2  A: 

Nope, it's a classic python gotcha :

fields=('image')

'image' is not a tuple, it's just a parenthesized string. Guess what happens, the framework tries to iterate through your string instead of iterating through the tuple ( 'i', 'm', 'a', 'g', 'e' ).

a valid tuple with only one element is written with an additional comma:

 fields=('image',)
vincent
Run into that before and looked right over it, already gotten past this but will make a good reference for the future. My own and anyone with a similar issueThank you
Alvin