MEDIA_ROOT Django找不到图片 [英] MEDIA_ROOT Django do not find images
问题描述
我想在我的模板上显示* .png图像,但不能!所有其他页面的东西呈现良好(从http路径引导,css从本地文件)。我浪费了几个小时试图设置路径,但没有效果。似乎我尝试了所有我可以发明。请帮助。
settings.py
...
import os
'''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 =(
#'/用户/ 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':[
'd jango.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 ='/卷/存储/ _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','st atic-only')
STATIC_URL ='/ static /'
STATICFILES_DIRS =(
os.path.join(os.path.dirname(os.path .dirname(os.path.dirname(__ file__))),'static','static'),
)
...
all.html
.....
/ pre>
{%block content%}
{%for products in products%}
< div class ='container-fluid'>
< div class ='col-md-6'> {{product.title}}
{{product.price}}
{% .productimage_set.all%}
< img src ='{{MEDIA_URL}} {{image}}'/>
< / div>
{%endfor%}
< / div>
{%endfor%}
{{image}}
{%endblock%}
.......
电子商务/ urls.py
from django.conf导入设置
从django.conf.urls导入模式,包括,url
从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'),
)
产品来自django.conf的
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
urlpatterns = patterns('products.views',
url(r'$','all_products',name ='products'),
)
models.py
从django.db导入模型
类产品(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)
已更新= models.DateTimeField auto_now = True,auto_now_add = False)
def __unicode __(self):
return self.title
'''sort item alphabeti cally'''
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)
已更新= models.DateTimeField(auto_now = True,auto_now_add = False)
def __unicode __(self):
return self.image
< img src ='{{MEDIA_URL}} {{image}}'/>
是错误的。您需要
< img src ='{{MEDIA_URL}} {{image}}'/>
或
< img src ='{{image.url}}'/>
由于 ImageField
继承所有的方法和属性 FileField
,您可以访问 FileField
属性名为 .url
调用 url()
方法存储
类。它将返回包含 / media /
文件夹的相对路径。
I want to display *.png images on my template but can`t ! All other page's stuff renders well (bootstrap on http path, css from local files). I waste few hours trying to set path but no effect. Seems I tried all I could invent. Please help.
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 %}
.......
ecommerce/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'),
)
products/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
<img src ='{{ "MEDIA_URL" }}{{ image }}'/>
is wrong. You need
<img src ='{{ MEDIA_URL }}{{ image }}'/>
or
<img src ='{{ image.url }}'/>
Since ImageField
inherits all methods and attributes of FileField
, you have an access to FileField
property called .url
which calls the url()
method of Storage
class. It will return the relative path including /media/
folder.
这篇关于MEDIA_ROOT Django找不到图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!