Re: Validating and rendering dynamic form

2009-01-31 Thread Louis Sayers

Ok... maybe not so solved.
Now ony the last image submitted is validated.  in the _clean_photo
(self, dataName) method, I print the data, and all the images except
for the last one contain None.

Any Ideas?

On Feb 1, 12:51 pm, DragonSlayre  wrote:
> Solved my problem - data = self.cleaned_data[dataName]  should have
> been:
> data = self.cleaned_data.get(dataName)
>
> Silly me
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Validating and rendering dynamic form

2009-01-31 Thread Louis Sayers

For some reason, _clean_photo is being given the last image name for
dataName, and that's why only the last picture is being validated.

The line of code that calls _clean_photo with the image name is:
setattr(new_form, 'clean_' + image_name, lambda self: self._clean_photo
(image_name))

This line is within the make_image_form method:

 for image_name in image_fields:
print "image name is: ", image_name
setattr(new_form, 'clean_' + image_name, lambda self:
self._clean_photo(image_name))


You can see that I'm printing the image name.  The image_name is
changing, but it looks like only the final image_name is used (which
will of course be the last image).  Why would this be happening?  All
of the clean methods are generated correctly (e.g. clean_pic_1,
clean_pic_2)

Is there something to do with lambda expressions that I'm missing?



Thanks,

Louis
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Validating and rendering dynamic form

2009-01-31 Thread Louis Sayers

answer from stackoverflow:
http://stackoverflow.com/questions/499964/how-do-you-create-python-methodssignature-and-content-in-code/499982#499982

Python code behaves like this for functions defined in scope of
methods. Use this instead:

for image_name in image_fields:
print "image name is: ", image_name
setattr(new_form, 'clean_' + image_name,
lambda self, iname=image_name: self._clean_photo(iname))

The usage of default keyword argument makes Python remember it at the
time of lambda function creation rather than at the time of its
calling (when it would always take the last image).

yay :)

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Error saving a Model with a custom image field

2009-02-02 Thread Louis Sayers

I have an Image model:

class Image(models.Model):
photo = ImageWithThumbsField(
upload_to=image_upload_location,
sizes=((thumb_widths,thumb_heights),))

The ImageWithThumbsField is something I got from 
http://code.google.com/p/django-thumbs/
It seems to work fine when I have a ModelForm created from the Image
model, but at the moment I'm trying to create a Image object, and save
it - so I do this:

new_image = ImageWithThumbsField(images_to_save[image],
upload_to=ListingModels.image_upload_location,
sizes=((ListingModels.thumb_widths,
ListingModels.thumb_heights),))
image_object= Image(photo=new_image)
image_object.save()


The ImageWithThumbsField class extends django.db.models.ImageField,
and sets another class 'ImageWithThumbsFieldFile' as the attr_class.

I'm guessing that the attr_class is used when the Model is saved -as
the ImageWithThumbsFieldFile __init__ method is called when the model
is being saved:

class ImageWithThumbsFieldFile(ImageFieldFile):
"""
See ImageWithThumbsField for usage example
"""
def __init__(self, *args, **kwargs):
super(ImageWithThumbsFieldFile, self).__init__(*args,
**kwargs)
self.sizes = self.field.sizes

if self.sizes:
def get_size(self, size):
if not self:
return ''
else:
split = self.url.rsplit('.',1)
thumb_url = '%s.%sx%s.%s' % (split[0],w,h,split
[1])
return thumb_url

for size in self.sizes:
(w,h) = size
setattr(self, 'url_%sx%s' % (w,h), get_size(self,
size))


The Line that causes the failure is in ImageWithThumbsFieldFile, and
is the  split = self.url.rsplit('.',1) line.

Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py"
in get_response
  86. response = callback(request, *callback_args,
**callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/auth/
decorators.py" in __call__
  67. return self.view_func(request, *args, **kwargs)
File "/home/louis/uniListings/../uniListings/listings/views.py" in add
  63. imageModel.save()
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in
save
  311. self.save_base(force_insert=force_insert,
force_update=force_update)
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in
save_base
  371. values = [(f, f.get_db_prep_save(raw and getattr
(self, f.attname) or f.pre_save(self, True))) for f in
meta.local_fields if not isinstance(f, AutoField)]
File "/usr/lib/python2.5/site-packages/django/db/models/fields/
__init__.py" in pre_save
  179. return getattr(model_instance, self.attname)
File "/usr/lib/python2.5/site-packages/django/db/models/fields/
files.py" in __get__
  122. instance.__dict__[self.field.name] =
self.field.attr_class(instance, self.field, file)
File "/home/louis/uniListings/listings/thumbs.py" in __init__
  118. setattr(self, 'url_%sx%s' % (w,h), get_size
(self, size))
File "/home/louis/uniListings/listings/thumbs.py" in get_size
  112. split = self.url.rsplit('.',1)
File "/usr/lib/python2.5/site-packages/django/db/models/fields/
files.py" in _get_url
  54. return self.storage.url(self.name)
File "/usr/lib/python2.5/site-packages/django/core/files/storage.py"
in url
  213. return urlparse.urljoin(self.base_url, name).replace('\
\', '/')
File "/usr/lib/python2.5/urlparse.py" in urljoin
  253. urlparse(url, bscheme, allow_fragments)
File "/usr/lib/python2.5/urlparse.py" in urlparse
  154. tuple = urlsplit(url, scheme, allow_fragments)
File "/usr/lib/python2.5/urlparse.py" in urlsplit
  193. i = url.find(':')

Exception Type: AttributeError at /listings/add/
Exception Value: 'ImageWithThumbsField' object has no attribute 'find'


I'm really struggling with this error, if anyone has any ideas, I'll
be glad to hear them :)

Thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Error saving a Model with a custom image field

2009-02-02 Thread Louis Sayers

I'm running the official release of django 1.02
Do you think there'd be any problems with this version?

I've only been using django for the last 2 months, so I'm fairly new
to it, and haven't investigated different revisions etc

On Feb 3, 12:58 pm, Andrew Ingram  wrote:
> Louis Sayers wrote:
> > I have an Image model:
>
> ...
> > Exception Type: AttributeError at /listings/add/
> > Exception Value: 'ImageWithThumbsField' object has no attribute 'find'
>
> > I'm really struggling with this error, if anyone has any ideas, I'll
> > be glad to hear them :)
>
> > Thanks
>
> What version of Django are you using? Revision 9766 of trunk breaks
> quite a lot of stuff to do with ImageFields and it hasn't been fixed
> yet. Basically anything which requires handling the file prior to saving
> in admin (calculating dimensions for example) doesn't work and will
> cause an exception.
>
> I've rolled back to revision 9765 for the time being.
>
> Regards,
> Andrew Ingram
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Error saving a Model with a custom image field

2009-02-02 Thread Louis Sayers

I think it must be something that I'm not doing.
It all works when I'm using a ModelForm, so there must be a difference
between what I'm doing manually, and what the ModelForm does for me.

The culprit must be my code:

new_image = ImageWithThumbsField(images_to_save[image],
upload_to=ListingModels.image_upload_location,
sizes=((ListingModels.thumb_widths,
ListingModels.thumb_heights),))
image_object= Image(photo=new_image)
image_object.save()


most likely the new_image creation I would think.

In all honesty, I'm not sure if I'm giving the ImageWIthThumbsField
class the image correctly - perhaps my brain is fried(mmm... bacon),
but how exactly do you give an ImageField (ImageWIthThumbsField
extends ImageField) class the actual Image ?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



removing cached form fields

2009-02-09 Thread Louis Sayers

I've got a django Form which contains a dictionary of strings. I've
given the form a submit button and a preview button. When the preview
button is pressed after entering some information, a POST is sent, and
the strings in the dictionary are automagically recovered (I assume
that it's done using session state or something). This is great,
exactly what I wanted.

The problem is that if I don't submit the form, then do a GET (i.e.
browse to the page with the form on it), enter some info and hit
preview, the information that was stored in the dictionary from the
first preview is still there.

How do you clear this information?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: removing cached form fields

2009-02-09 Thread Louis Sayers

Ok, I'm not doing any django for the next couple of days, so will post
when I have a watered down example (unless of course doing this solves
the problem :))

On Feb 10, 4:36 pm, Malcolm Tredinnick 
wrote:
> On Mon, 2009-02-09 at 17:17 -0800, Louis Sayers wrote:
> > I've got a django Form which contains a dictionary of strings. I've
> > given the form a submit button and a preview button. When the preview
> > button is pressed after entering some information, a POST is sent, and
> > the strings in the dictionary are automagically recovered (I assume
> > that it's done using session state or something). This is great,
> > exactly what I wanted.
>
> This is in no way default behaviour. So what you are doing in your
> specific code is important here. Simplify your code to a very simple
> example that demonstrates the problem and then we can see what you're
> trying to do.
>
> Regards,
> Malcolm
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



What is the best way to keep state between posts?

2009-02-12 Thread Louis Sayers

Howdy,

I've created a form that allows a user to post images to the server.
Once the images are posted, I am saving them to disc, and if there are
any errors, I return the form with the errors in them, and thumbnails
of the successfully uploaded pictures.

If they are unsuccessful when they post again, I need to still know
what images they have already successfully uploaded (so I can still
display their thumbnails.

I'm guessing that hidden fields in the form might be a good way to
tell which images they've uploaded, or perhaps saving the information
in session variables, and clearing these when I receive a get rather
than a post.


I'm interested to hear what method other django users might prefer?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: bookmarks app with images

2009-02-12 Thread Louis Sayers

Firstly, make sure that you have the enctype="multipart/form-data"
attribute on your form in your template.

You can create an object of your form by writing:
formObject = BookmarkSaveForm(request.POST, request.FILES)

test if it's valid:
if formObject.is_valid():


Looking at your Photo model, you are probably wanting a modelForm
instead of creating a new form.  If you haven't already seen the
documentation at djangoproject.com under the documentation tab, now
might be a good place to look.  For modelForms, there's a good bit of
doc at 
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#topics-forms-modelforms

I hope this helps at least a little.


On Feb 13, 9:23 am, Tonu Mikk  wrote:
> Hello, I am having trouble writing a view to upload images. I am
> creating a bookmarks application with Django 1.02 that would have a
> screenshot for each bookmark.  I extended the model in this way to
> include the images:
>
> class Photo(models.Model):
>     title = models.CharField(max_length=50)
>     photo = models.ImageField(upload_to="photos/")
>     bookmarks = models.OneToOneField(Bookmark, primary_key=True)
>
> The forms.py file has the following code related to images:
> class BookmarkSaveForm(forms.Form):
>   photo = forms.ImageField(
>     label='Upload screenshot',
>     required=False
> )
>
> I am not quite sure how to write views.py code for the saving the images
> to the specified directories.  Any suggestions on how to get started
> with this is appreciated.
>
> Thank you,
> Tonu
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---