Django管理员通过ImageField链接到图像 [英] Django admin link to image via ImageField
问题描述
我已经设置了一个包含以下字段的活动的Django模型:
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)
通过管理界面,我可以将正确放置在主目录的thumbs文件夹中的图像上传。当我尝试编辑我创建的活动时,界面说:目前:thumbs / image.png
这是一个超链接,指向 http:/ /localhost:8000/media/thumbs/image.png
。当我点击这个链接,我得到404页面找不到错误。如何让链接正确地指向我上传的图像?如果可能,如何直接在管理界面中显示图像?
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?
编辑:
MEDIA_ROOT = '/用户/.../ mysite的/媒体/'
;
MEDIA_URL = 'http:// localhost:8000 / media /'
;
MEDIA_ROOT = '/Users/.../mysite/media/'
;
MEDIA_URL = 'http://localhost:8000/media/'
;
URL的内容.py:
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)),
)
谢谢!
推荐答案
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_',)
这篇关于Django管理员通过ImageField链接到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!