MEDIA_ROOT Django找不到图像

时间:2015-11-08 09:09:52

标签: django django-templates media-queries

我想在我的模板上显示* .png图像,但不能!所有其他页面的东西渲染得很好(http路径上的bootstrap,本地文件中的css)。我浪费了几个小时试图设置路径但没有效果。似乎我尝试了所有我能发明的东西。请帮忙。

please see project tree

settings.py

... 
import os
 '''path settings for django==1.8'''
# BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))

TEMPLATE_DIRS = (
    #'/Users/jmitch/Desktop/seven/static/templates/',
    os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), "static", "templates"),
)

TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            # 'DIRS': [BASE_DIR+"/templates", ],
            'DIRS': [BASE_DIR + "/static/templates/", ],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                    'django.core.context_processors.media',
                ],
            },
        },
    ]
ENV_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
MEDIA_ROOT = os.path.join(ENV_PATH, 'media')
# MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))), 'static', 'media')
# MEDIA_ROOT = '/Volumes/Storage/_codework/e_shop/static/media/product/images/'
# MEDIA_ROOT = os.path.join(BASE_DIR,"media/images")
MEDIA_URL = '/media/'

STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'static', 'static-only')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'static', 'static'),
)

      ...

all.html

    .....    
 {% block content %}

  {% for product in products %}
      <div class='container-fluid'>
      <div class='col-md-6'>{{ product.title }}
      {{ product.price }}

      {% for image in product.productimage_set.all %}
           <img src ='{{ "MEDIA_URL" }}{{ image }}'/>
      </div>
      {% endfor %}
      </div>
  {% endfor %}
            {{ image }}
  {% endblock %}
  .......

电子商务/ urls.py

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.STATIC_ROOT
        }),
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT
        }),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^products/', include('products.urls')),
    url(r'^contact/', 'contact.views.contact_us', name='contact_us'),
)

产品/ urls.py

from django.conf import settings
from django.conf.urls import patterns, include, url

urlpatterns = patterns('products.views',

   url(r'$', 'all_products', name='products'),

  )

models.py

    from django.db import models


class Product(models.Model):
    title = models.CharField(max_length=220)
    description = models.CharField(max_length=3000, null=True, blank=True)
    price = models.DecimalField(max_digits=1000, decimal_places=2, null=True, blank=True)
    slug = models.SlugField()
    active = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)


    def __unicode__(self):
        return self.title

    ''' sort item alphabetically '''
    class Meta:
        ordering = ['title']

class ProductImage(models.Model):
    product = models.ForeignKey(Product)
    description = models.CharField(max_length=3000, null=True, blank=True)
    image = models.ImageField(upload_to='/product/images')
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)


    def __unicode__(self):
        return self.image

1 个答案:

答案 0 :(得分:0)

<img src ='{{ "MEDIA_URL" }}{{ image }}'/>

错了。你需要

<img src ='{{ MEDIA_URL }}{{ image }}'/>

<img src ='{{ image.url }}'/>

由于ImageField继承了FileField的所有方法和属性,因此您可以访问名为FileField的{​​{1}}属性,该属性调用.url url()方法1}}类。它将返回包含Storage文件夹的相对路径。