3

我已经建立了一个活动的 Django 模型,其中包含以下字段:

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

通过管理界面,我可以上传正确放置在主目录的 thumbs 文件夹中的图像。当我尝试编辑我创建的活动时,界面显示:Currently: thumbs/image.png这是一个指向http://localhost:8000/media/thumbs/image.png. 当我点击这个链接时,我得到一个 404 page not found 错误。如何让链接正确指向我上传的图片?如果可能的话,我怎样才能直接在管理界面中显示图像?

编辑:

媒体根 = '/Users/.../mysite/media/'; 媒体网址 = 'http://localhost:8000/media/';

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)),
)

谢谢!

4

1 回答 1

9

设置.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()

模型.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_',)
于 2013-03-01T13:09:11.830 回答