26 lines
809 B
Python
26 lines
809 B
Python
from django.conf import settings
|
|
from django.db import models
|
|
from django.urls import reverse
|
|
|
|
|
|
class Image(models.Model):
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
modified = models.DateTimeField(auto_now=True)
|
|
|
|
file = models.ImageField(upload_to='image', null=True, blank=True)
|
|
|
|
def get_absolute_url(self):
|
|
return '/' + settings.URL_PREFIX + self.file.url[1:]
|
|
get_absolute_url.short_description = 'URL'
|
|
|
|
def image_tag(self):
|
|
from django.utils.html import mark_safe
|
|
return mark_safe('<img src="%s" style="max-width:256px" />' % self.get_absolute_url())
|
|
image_tag.short_description = 'Preview'
|
|
|
|
def __str__(self):
|
|
if self.file:
|
|
name = self.file.name.split('/')[-1]
|
|
else:
|
|
name = 'None'
|
|
return name
|