Forums

Not saving default image

there is my model which has an image field and i want a default image sets if user didnt enter any

class Parts(models.Model):
....
pic=models.ImageField(blank=True,null=True,verbose_name=_('pic'),upload_to = 'stocks', default = 'stocks/nopic.jpg')
.....

but when the user dosent enter an image , it sets this path for image "stocks/nopic.jpg" but can not open the path amd show it in template and when i manually brows this path :"stocks/nopic.jpg" and enter the image , it saves something like : stocks/nopic_yJNuXZn.jpg or stocks/nopic_bOzePAk.jpg and it is showable

How can i define a default image for imagefield ?

I suspect this might be related to the problems with your static file configuration that we're discussing in this thread.

what are these suffix which are added to the name of the picture ? this method of adding suffix doesnt let me to have default pic ?

and all the new pictures with new suffixes are saved in the database and this make my database overflow

and also most of the time the page needs to refresh one or two times to show all the pictures ?

I think Django is mangling the filenames because you're specifying that they're in the media directory. For the default image, perhaps try putting it in the media directory, like this:

pic = models.ImageField(blank=True, null=True, verbose_name=_('pic'), upload_to = 'stocks', default='/static/nopic.jpg')

...note the / at the start of the default path. You'll also need to put the default file, nopic.jpg, into the static files directory.

I think it had problem with the name of the picture , when i tried to open the pic via the path , showed this error

The image http://zg500.pythonanywhere.com/media/stocks/nopic.jpg can not be displayed becouse it contains errors

so i uploaded an image with the nopic_1.jpg name and also deleted " / " from the default path , and its ok now

Cool. Glad you worked it out.