26 lines
744 B
Python
26 lines
744 B
Python
|
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 self.file.url
|
||
|
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
|