[英]Visualizing uploaded images in Django Admin
雖然我找到了關於如何在 Django-Admin 中正確顯示圖像的有用帖子( #1 、 #2 、 #3 ),但到目前為止我還沒有成功地遵循這些建議。 正如您在下面看到的,上傳的圖像沒有正確顯示,並且顯示 404 錯誤。
下圖說明了雖然上傳的圖片已經成功上傳到服務器,但沒有顯示的事實:
編輯給定對象時,我還想知道如何顯示縮略圖而不是圖像的路徑: 下面,您可以找到我迄今為止一直在使用的配置:
設置.py
import os
PROJECT_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..').replace('\\','/')
SITE_ROOT = PROJECT_ROOT
# Path to media files, e.g. images
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'
網址.py
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings
from . import views
urlpatterns = patterns('',
# some url configs here ... removed for simplification purposes ;-)
)
if settings.DEBUG is True:
urlpatterns += patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}) )
#urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
模型.py
class Image(models.Model):
title = models.CharField(db_column='title', max_length=180, blank=True, null=True, help_text="Title of the image")
imagefile = models.ImageField(db_column='imagefile', upload_to='images', null=True, blank=True, help_text="Load an image.")
#... some other fields here, removed for simplification purposes ;-)
def image_(self):
return '<a href="/media/{0}"><img src="/media/{0}"></a>'.format(self.imagefile)
image_.allow_tags = True
class Meta:
managed = False
db_table = 'image'
def __unicode__(self):
return u'%s' % (self.title)
管理員.py
from django.contrib import admin
from .models import Image
class ImageAdmin(admin.ModelAdmin):
list_display = ('title', 'description', 'image_', )
search_fields = ('title', 'description')
list_per_page = 25
admin.site.register(Image, ImageAdmin)
你有沒有嘗試過這樣的事情:
管理文件
from django.contrib import admin
from .models import Image
class ImageAdmin(admin.ModelAdmin):
list_display = ('title', 'description', 'image_display', )
search_fields = ('title', 'description')
list_per_page = 25
def image_display(self, obj):
return '<a href="/media/{0}"><img src="/media/{0}"></a>'.format(self.imagefile.url)
image_.allow_tags = True
image.short_descripition = u'IMAGE'
admin.site.register(Image, ImageAdmin)
那總是對我有用。 如果這有幫助,請告訴我。
這個答案至少延遲了一年,但是,如果你仍然有同樣的問題,你可以像這樣修復它:
首先,讓我們假設您的 django 項目具有這種架構並且您正在運行django 2.0 :
. django_test
|_ django_test
| |_ __init__.py
| |_ settings.py
| |_ urls.py
| |_ wsgi.py
|_ media
| |_ author_headshot
|_ my_app
| |_ admin.py
| |_ __init__.py
| |_ apps.py
| |_ migrations
| |_ models.py
| |_ tests.py
| |_ urls.py
| |_ views.py
|_ static
|_ templates
然后,添加您的django_test/settings.py :
# static files will be stored in 'static' folder
STATIC_URL = '/static/'
STATICFILES_DIR = [
BASE_DIR + '/static'
]
# media files will be stored in 'media' folder
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR + '/media'
然后,在django_test/urls.py 中確保添加MEDIA_URL和MEDIA_ROOT的靜態路徑,如下例所示:
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
# More paths/urls
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
最后,在您的應用程序模型中,您可以為每個 ImageField 添加一個唯一的文件夾,您的媒體文件將在其中存儲,如下例所示:
在my_app/models.py 中:
from django.db import models
from django.utils.html import format_html
class Author(models.Model):
# My models ...
# Here, the media files will be stored in my_project/media/author_headshot
headshot = models.ImageField(upload_to='author_headshot')
# If you want to show the media files in the list display in the admin panel:
def image_tag(self):
return format_html('<img href="{0}" src="{0}" width="150" height="150" />'.format(self.headshot.url))
image_tag.allow_tags = True
image_tag.short_description = 'Image'
然后,在my_app/admin.py 中:
from django.contrib import admin
from . import models
@admin.register(models.Author)
class AuthorAdmin(admin.ModelAdmin):
list_display = ('image_tag')
最后,您的媒體文件將具有以下網址:
django 管理面板的屏幕截圖:
有細線條當您使用的ImageField,當你使用函數image_(個體經營),以使圖像可見。 見下圖:這是圖片的鏈接,因為我的聲譽不允許我發布圖片。 img1列代表您在models.py 中使用的imagefile變量。
ImageField的圖像保存在圖像文件夾中,該文件夾是your-project/media/images 目錄。
當您使用“ media/{0} ”時,您的image_(self)函數指向更深的一層,即它將指向your-project/media//media/images (注意這里的 // 由於額外/ / {0}) 。 您的函數應修改如下:
def image_tag(self):
return '<a href="{0}"><img src="{0}" width = "120" height = "120"></a>'.format(self.img1.url)
image_tag.allow_tags = True
image_tag.short_description = 'y-Image'
如果您是 Chrome 用戶,請嘗試“Chrome 的 Web 服務器”擴展程序,您可以在此處找到: https<\/a> :\/\/stackoverflow.com\/a\/46480984\/14508690
"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.