图像未显示在Django管理网站中 [英] image not displaying in django admin site

查看:68
本文介绍了图像未显示在Django管理网站中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个模型:

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 /

But image is not loading on the admin page and the image URL is : http://localhost:8000/admin/couponRestApiApp/stores/static/mcDonalds.jpg/

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

is showing but the correct path should be: /home/vaibhav/TRAC/coupon-rest-api/couponRestApi/couponRestApiApp/stores/static/mcDonalds.jpg/

必须将图像存储在何处,以便它们显示在Django管理页面上

Where should the images must be stored so that they will be displayed on the Django admin Page

推荐答案

定义 MEDIA_ROOT MEDIA_URL 正确设置。

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



models.py

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



urls.py



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

尝试使用上面的代码。

在评论中提问的答案:

是添加到模型名称的,因为将有多个模型实例。要摆脱它,请为模型定义 verbose_name

's' is being added to the model name, since there will be multiple model instances. To get rid of it, define verbose_name for the model.

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

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

这篇关于图像未显示在Django管理网站中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆