gpt4 book ai didi

python - 在 Django Admin 中可视化上传的图像

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:09 25 4
gpt4 key购买 nike

虽然我已经找到了关于如何在 Django-Admin 中正确显示图像的有用帖子(#1#2#3),但到目前为止我还没有成功地遵循这些建议。正如您在下面看到的,上传的图像未正确显示,并显示 404 错误。

下图说明上传的图片已经成功上传到服务器,但没有显示: Broken links for uploaded images

当我在断开的链接上单击鼠标时,浏览器上显示以下错误: URL Error

在编辑给定的对象时,我也想知道如何显示缩略图而不是图像的路径: URL to image下面,您可以找到我到目前为止一直使用的配置:

settings.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/'

urls.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)

admin.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)

最佳答案

这个答案至少延迟了一年,但是,如果你仍然有同样的问题,你可以这样解决:

首先,假设您的 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_URLMEDIA_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',)

最后,您的媒体文件将具有这种 url:

http://example.domain/media/file_path.extension

django 管理面板的屏幕截图:

enter image description here

enter image description here

关于python - 在 Django Admin 中可视化上传的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36177385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com