python - Thumbnail now showing in django admin list view -


i have created basic file upload app , i'd able view thumbnail of image on django admin list view. i've tried implementing code on blog post - http://www.acedevs.com/blog/2011/07/11/django-admin-list-view-thumbnails/ ).

after adding code, i've got field called 'slide thumbnail' when upload image sais 'none' it's converting image thumbnail , displaying it.

i don't have errors not sure i'm going wrong..

here code , hopfully can shed light.

modles:

from django.db import models sorl.thumbnail.main import djangothumbnail import os

class file(models.model):       category_choices = (         ('image', 'image'),         ('document', 'document')     )      title = models.charfield(max_length=400, help_text="enter title of file, appear on listings page")         file_type = models.charfield(choices=category_choices, help_text="optional, filtering on listings page.", max_length=200, blank=true, null=true, default=none)       image_upload = models.imagefield(upload_to="images/filesapp", height_field="image_height", width_field="image_width", blank=true, null=true)      file_upload = models.filefield(upload_to="pdf/filesapp", blank=true, null=true)      image_height = models.positiveintegerfield(null=true, blank=true, editable=false)      image_width = models.positiveintegerfield(null=true, blank=true, editable=false)        def slide_thumbnail(self, width=300, height=200):         if self.image:             thumb = djangothumbnail(self.image, (width, height))             return '{img src="%s" /}' % thumb.absolute_url         return '{img src="/media/img/admin/icon-no.gif" alt="false"}'     slide_thumbnail.allow_tags = true     def __unicode__(self):         return u'slide: %s - %sx%s' % (self.title, self.image_height, self.image_width) 

admin.py

from django.contrib import admin models import *   def delete_selected(modeladmin, request, queryset):     element in queryset:         element.delete() delete_selected.short_description = "delete selected elements"  class fileadmin(admin.modeladmin):     model = file     actions = [delete_selected]      list_display = ('title', 'file_type', 'slide_thumbnail')  admin.site.register(file, fileadmin) 

thanks!

it turns out variable wrong in function creating thumbnail. anyway, here finished code if interested in using thumbnails in django admin list view.

models.py

from django.db import models sorl.thumbnail.main import djangothumbnail import os

class file(models.model):

category_choices = (     ('image', 'image'),     ('document', 'document') )  title = models.charfield(max_length=400, help_text="enter title of file, appear on listings page")     file_type = models.charfield(choices=category_choices, help_text="optional, filtering on listings page.", max_length=200, blank=true, null=true, default=none)   image_upload = models.imagefield(upload_to="images/filesapp", height_field="image_height", width_field="image_width", blank=true, null=true)  file_upload = models.filefield(upload_to="pdf/filesapp", blank=true, null=true)  image_height = models.positiveintegerfield(null=true, blank=true, editable=false)  image_width = models.positiveintegerfield(null=true, blank=true, editable=false)    def slide_thumbnail(self, width=300, height=200):     if self.image_upload:         thumb = djangothumbnail(self.image_upload, (width, height))         return '<img src="%s" />' % thumb.absolute_url     return '{img src="/media/img/admin/icon-no.gif" alt="false"}' slide_thumbnail.allow_tags = true  def __unicode__(self):     return u'file: %s - %sx%s' % (self.title, self.image_height, self.image_width) 

admin.py

from django.contrib import admin models import *   def delete_selected(modeladmin, request, queryset):     element in queryset:         element.delete() delete_selected.short_description = "delete selected elements"  class fileadmin(admin.modeladmin):     model = file     actions = [delete_selected]      list_display = ('title', 'file_type', 'slide_thumbnail')  admin.site.register(file, fileadmin) 

please note: need sorl-thumbnail installed.

hope saves pain had go through!


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -