3

I've set up a Django model of an activity which contains following field:

class Activity(models.Model):
    ...
    thumbnail = models.ImageField(upload_to="thumbs/", blank=True, null=True)

Via the admin interface I can upload an image that is put correctly in the thumbs folder of the home directory. When I try to edit the activity I created, the interface says: Currently: thumbs/image.png which is a hyperlink that points to http://localhost:8000/media/thumbs/image.png. When I click this link, I get a 404 page not found error. How can I get the link to point correctly to the image I uploaded? And if possible, how can I display the image directly in the admin interface?

EDIT:

MEDIA_ROOT = '/Users/.../mysite/media/'; MEDIA_URL = 'http://localhost:8000/media/';

Contents of urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
)

Thank you!

CedricDeBoom
  • 183
  • 3
  • 10
  • The first part of making the link work is a symptom of a problem with your `urls.py`. Please update your question by clicking on the `edit` link and add the contents of your `urls.py` file. – Burhan Khalid Mar 01 '13 at 13:03
  • Don't hardcode the path, use import os instead – catherine Mar 02 '13 at 12:56

1 Answers1

9

settings.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

models.py

class Activity(models.Model):
    ...
    thumbnail = models.ImageField(upload_to="thumbs/", blank=True, null=True)

    def image_(self):
        return '<a href="/media/{0}"><img src="/media/{0}"></a>'.format(self.thumbnail)
    image_.allow_tags = True



class AdminName(admin.ModelAdmin):
    .........
    list_display = ('image_',)
catherine
  • 22,492
  • 12
  • 61
  • 85
  • This is a good solution but the settings part is a bit redundant, it should either use the `staticfiles_urlpatterns()` if you're using the staticfiles app or just use `static()` if you're not, as one calls the other already. – Samus_ Oct 01 '13 at 05:58