Question

I've got a Django model for a shirt. Here's the relevant part:

class Shirt(models.Model):
    front_image = models.ImageField(upload_to="shirt_fronts")

class ShirtAdmin(admin.ModelAdmin):
    pass

It works well in general; I can create new shirts in the admin, and upload pictures for them. I can then view those shirts in my shirt view, and they show up fine.

The only problem is in the admin itself. After I upload the image the image itself doesn't appear, only a link with the path to the image (eg. shirt_fronts/someImage.jpg). If I then click the link I get taken to the wrong path for the image which (unsurprisingly) doesn't work.

So my question is, how do I fix that? Specifically, how can I get the link to go to the right place or (better yet) get that link to be the image or a thumbnail version of it?

Était-ce utile?

La solution 2

I eventually got the image working with code from here: http://www.psychicorigami.com/2009/06/20/django-simple-admin-imagefield-thumbnail/

I created an admin_image_widget.py file:

from django.contrib.admin.widgets import AdminFileWidget
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe

class AdminImageWidget(AdminFileWidget):
    def render(self, name, value, attrs=None):
        output = []
        if value and getattr(value, "url", None):
            image_url = '/media/dynamic/' + value.url
            file_name=str(value)
            output.append(u' <a href="%s" target="_blank"><img src="%s" alt="%s" style="height: 100px; width: 100px;"/></a> %s ' % \
                (image_url, image_url, file_name, _('Change:')))
        output.append(super(AdminFileWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))

(NOTE: I modified the image URL and image tag. The image URL I could probably do better by using the static file code instead of hard-coding the path. The image tag I tweaked to limit the size of the thumbnail.)

I then moved my shirt admin class in to admin.py and made it:

class ShirtAdmin(admin.ModelAdmin):

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'front_image':
            request = kwargs.pop("request", None)
            kwargs['widget'] = AdminImageWidget
            return db_field.formfield(**kwargs)
        return super(ShirtAdmin,self).formfield_for_dbfield(db_field, **kwargs)

admin.site.register(Shirt, ShirtAdmin)

Together this got me in-admin thumbnails! Thanks Psychic Origami blog for the code snippet, and thanks Bigcortex for getting me looking in the right direction.

Autres conseils

Django doesn't show the thumbail by defaut. There has been several answers on how to show an upload image in the django admin.

This should help you

As for the broken links, django doesn't serve media files out of the box. You can however configure it to serve them in debug mode.

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

For more information on how to serve static files in django

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top