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?