- Author:
- agusmakmun
- Posted:
- March 18, 2016
- Language:
- Python
- Version:
- 1.7
- Score:
- 1 (after 1 ratings)
Dynamic upload File type for Gallery Admin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | """
>>> from blog.models import Gallery
>>>
>>> Gallery.objects.all()[0].file_type()
'<img height="40" width="60" src="gallery/2016/03/17/dhclient.png"/>'
>>>
>>> Gallery.objects.all()[0].field_uploaded.url
'gallery/2016/03/17/dhclient.png'
>>>
>>> Gallery.objects.all()[0].get_absolute_url()
'<a href="http://127.0.0.1:8000/gallery/2016/03/17/dhclient.png" target="_blank">http://127.0.0.1:8000/gallery/2016/03/17/dhclient.png</a>'
>>>
>>>
"""
### ==================
### models.py
### ==================
from django.db import models
class Gallery(models.Model):
title = models.CharField(max_length=200, help_text="Type title of file or image")
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
TIPE = (
('image','Image'),
('file','File or Documents')
)
type_for_file = models.CharField(max_length=200, choices=TIPE, default='image', help_text="Please Choice only One Field, Image or Files")
field_uploaded = models.FileField(upload_to='gallery/%Y/%m/%d/')
IMAGES = ['.jpg', '.png', '.jpeg', '.gif']
def file_type(self):
if self.type_for_file == 'image':
import os
fx = str(os.path.splitext(str(self.field_uploaded.url))[1])
if fx in self.IMAGES:
return ('<img height="40" width="60" src="%s"/>' % self.field_uploaded.url)
elif self.type_for_file == 'file':
return '<img height="40" width="45" src="/static/asset/icons/file-icon.png"/>'
file_type.short_description = 'Type'
file_type.allow_tags = True
domain = 'http://127.0.0.1:8000/'
def get_absolute_url(self):
return '<a href="'+self.domain+self.field_uploaded.url+'" target="_blank">'+self.domain+self.field_uploaded.url+'</a>'
get_absolute_url.short_description = 'Absolute Url'
get_absolute_url.allow_tags = True
def save(self, *args, **kwargs):
super(Gallery, self).save(*args, **kwargs)
if not self.file_type():
raise Exception('Could not uploaded- is the file type valid?')
def __unicode__(self):
return self.title
class Meta:
verbose_name = "Gallery Entry"
verbose_name_plural = "Gallery and Files"
ordering = ["-created"]
### ==================
### admin.py
### ==================
from django.contrib import admin
from . import models
class GalleryAdmin(admin.ModelAdmin):
list_display = ("file_type", "title", "get_absolute_url", "created")
search_fields= ['title']
list_filter = ['created']
list_per_page = 5
admin.site.register(models.Gallery, GalleryAdmin)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.