Seperate Image Model

18 views
Skip to first unread message

Josh

unread,
Aug 7, 2011, 7:33:07 AM8/7/11
to django...@googlegroups.com
I'm only working a few weeks with Django and I want some advice about handling images. 

Conceptual I have to remind myself that Django is pure python, so that makes me complicate things sometimes. Also I still have some conceptual difficulties with templates and template_tags. Also I'm not really a programmer or webdesigner. I'll just describe my approach and finish with my problems. 

In my models I want to use a seperate model for Images that are displayed with content, because content might have more than one image connected to them. The images can be local on a mediaserver or taken from an external URL.


class Image(models.Model):
    image = models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100, unique=True, blank=True, null=True,)
    thumbnail = models.ImageField(upload_to=None, height_field=80, width_field=120, max_length=100, unique=True, blank=True, null=True,)
    urlimage = models.URLField(verify_exists=True, max_length=200, blank=True, null=True,)
    smallthumbnail = models.ImageField(upload_to=None, height_field=40, width_field=60, max_length=100, unique=True, blank=True, null=True,)
    entry = models.ManyToManyField(Entry, blank=True, null=True, default = None)
    urlexists = modelf.IntegerField(default=0)

    def save(self, force_insert=False, force_update=False):
        if self.urlimage.verify_exists == True:
            #test to check: 0 = False, 1 = True
            self.urlexists = 1
            
    ...


Images will be uploaded and thumbnailed beforehand. The images on the mediaserver have the same name and are split in 'full/', 'smallthumbnail/' and 'thumbnail/' directories. When displaying the content I'll retrieve the corresponding images and recreate the url using {{ MEDIA_URL }} and the respective directories. The html will be written in the template.

My problem is what the best way to achieve this? My solution is below in (pseudo) code. I want to use get_absolute_url to achieve this.

@models.permalink
def get_absolute_url(self):
    
    if self.urlimage.verify_exists == True:
        return ('templatename', (), { 'urlimage': self.urlimage, } 
                )

    elif len(self.image) > 0:
        #check if 
        return ('template_name', (), { 'image': self.image, 'thumbnail': self.thumbnail, 
                                        'smallthumbnail': self.smallthumbnail }
                )

    else:
        #Can this check be used to delete the record from the database and how do I do this?
        remove-from-table            
        return None

Am I complicating things or are there better alternatives to achieve this?

Gmail - neonmark

unread,
Aug 7, 2011, 6:34:38 PM8/7/11
to django...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/hC6kwkOXbncJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Gmail - neonmark

unread,
Aug 7, 2011, 6:38:27 PM8/7/11
to django...@googlegroups.com

Josh

unread,
Aug 7, 2011, 7:05:30 PM8/7/11
to django...@googlegroups.com
The images and creation of thumbnails aren't the problem. I'll look into the 1th and 3rd.  I've looked at a lot of image apps that might give a solution to my problem, but none fitted my requirements.

I'm not sure how to get the images in the content. There might be no image at all, but also one or more. I've also been thinking about using markdown or creating an additional textfield in the model. Maybe I can put the img.id or img.filename in there (e.g. some text [[img.id, width=100, etc]] and some more text

Josh

unread,
Aug 7, 2011, 7:10:06 PM8/7/11
to django...@googlegroups.com
Maybe I should add I don't want to manually assign the images, but script this as much as possible. It involves 1000s of images.
Reply all
Reply to author
Forward
0 new messages