【问题标题】:image not displaying in django admin site图像未显示在 django 管理站点中
【发布时间】:2020-07-18 01:13:09
【问题描述】:

我有这个模型:

projectDirPath = path.dirname(path.dirname(__file__)) 
storeImageDir = FileSystemStorage(location=projectDirPath + '/couponRestApiApp/stores')

class stores(models.Model):
    """ This is the store model """
    storeName = models.CharField(max_length=15)                                          # Store Name
    storeDescription = models.TextField()                                                # Store Description
    storeURL = models.URLField()                                                         # Store URL
    storePopularityNumber = models.IntegerField(max_length=1)                            # Store Popularity Number  
    storeImage = models.ImageField(upload_to="images",storage=storeImageDir)            # Store Image 
    storeSlug = models.CharField(max_length=400)                                         # This is the text you see in the URL
    createdAt = models.DateTimeField(auto_now_add=True)                                  # Time at which store is created
    updatedAt = models.DateTimeField(auto_now=True)                                      # Time at which store is updated
    storeTags = models.ManyToManyField(tags)                                             # All the tags associated with the store

    def __unicode__(self):
        return unicode(self.storeName)

    def StoreTags(self):
        return '\n'.join([s.tag for s in self.storeTags.all()])
    def StoreImage(self):    
        return '<img src="%s" height="150"/>' % (self.storeImage)
    StoreImage.allow_tags = True

但是管理页面上没有加载图像,图像 URL 是:http://localhost:8000/admin/couponRestApiApp/stores/static/mcDonalds.jpg/

正在显示,但正确的路径应该是:/home/vaibhav/TRAC/coupon-rest-api/couponRestApi/couponRestApiApp/stores/static/mcDonalds.jpg/

图像必须存储在哪里才能显示在 Django 管理页面上

【问题讨论】:

  • 找不到页面 (404) 请求方法:GET 请求 URL:localhost:8000/admin/couponRestApiApp/stores/static/… 存储具有主键 u'static/mcDonalds.jpg' 的对象不存在。每当我尝试打开图像时都会显示此错误..
  • 为什么upload_toFileSystemStorage一起使用?
  • 如果有另一种方法,那就告诉...
  • 你使用的django版本是什么?
  • Django 版本:(1, 4, 5, 'final', 0)

标签: python django


【解决方案1】:

在您的设置中正确定义MEDIA_ROOTMEDIA_URL

settings.py

import os
CURRENT_PATH = os.path.abspath(os.path.dirname(__file__).decode('utf-8'))

MEDIA_ROOT = os.path.join(CURRENT_PATH, 'media').replace('\\','/')

MEDIA_URL = '/media/'

models.py

storeImage = models.ImageField(upload_to="images")

urls.py

from django.conf import settings
urlpatterns += patterns('',
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
)

尝试使用上面的代码。


回答评论中提出的问题:

's' 被添加到模型名称中,因为会有多个模型实例。要摆脱它,请为模型定义 verbose_name

class stores(models.Model):
    .....
    storeName = models.CharField(max_length=15) 
    .....

    class Meta:
        verbose_name        = 'Store'
        verbose_name_plural = 'Stores'

【讨论】:

  • 我通过提及尝试解决方案:MEDIA_URL = '/stores/images/' 和 MEDIA_URL = '/media/' 但不起作用..
  • 尝试在您的urls.py 中添加上述网址格式。
  • 您的解决方案正在运行,但我的意思是如何停止 url 回溯。任何人都可以看到上传文件夹中的所有文件...如何阻止它
  • 在 url 模式中,指定 'show_indexes': False 而不是 True。这将阻止显示文件索引。
  • 谢谢伙计,其实我是 Django 的新手,所以我还在学习...感谢您的帮助...
【解决方案2】:

根据 Django 文档,对于 Django 1.11 及更高版本,您需要重写:

urls.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

【讨论】:

    【解决方案3】:

    在您的 HTML 代码中调用全局 url 像 {{name.store.url}} 它应该在您的 HTML 代码上完成

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 2011-01-24
      • 2014-07-17
      • 2015-11-19
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      相关资源
      最近更新 更多