我正在使用django 1.11
。
我无法在页面上显示我的图像文件;它说
media/products/file-name does not exits
但我检查过,我的 MEDIA_URL
和 MEDIA_ROOT
是正确的。
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS =[
os.path.join(BASE_DIR,"static_my_proj")
]
STATIC_ROOT = os.path.join(
os.path.dirname(BASE_DIR), "workspace/static_cdn", "static_root")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(
os.path.dirname(BASE_DIR), "workspace/static_cdn", "media_root")
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
from django.contrib import admin
from app import views
from products.views import (
ProductListView,
ProductDetailView,
ProductFeaturedListView,
ProductFeaturedDetailView,
ProductDetailSlugView,
)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home_page, name='home'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^login/$', views.login_page, name='login'),
url(r'^register/$', views.register_page, name='register'),
url(r'^product/$', ProductListView.as_view(), name='product'),
# url(r'^product/(?P<pk>\d+)/$', ProductDetailView.as_view(), name='detail'),
url(r'^product/(?P<slug>[\w-]+)/$', ProductDetailSlugView.as_view(), name='detail'),
url(r'^featured/$', ProductFeaturedListView.as_view(), name='featured'),
url(r'^featured/(?P<pk>\d+)/$', ProductFeaturedDetailView.as_view(), name='fdetail'),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_URL)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_URL)
models.py
def get_filename_ext(filepath):
base_name = os.path.basename(filepath)
name, ext = os.path.splitext(base_name)
return name, ext
def upload_image_path(instance,filename):
new_filename = random.randint(1,2324324423)
name, ext = get_filename_ext(filename)
final_filename = '{new_filename}{ext}'.format(
new_filename=new_filename, ext=ext)
return 'products/{new_filename}/{final_filename}'.format(
new_filename=new_filename, final_filename=final_filename)
class ProductManager(models.Manager):
def featured(self):
return self.get_queryset().filter(featured=True)
class Product(models.Model):
title = models.CharField(max_length = 120)
slug = models.SlugField(blank=True)
description = models.TextField()
price = models.DecimalField(decimal_places=2, max_digits=10, default=39.99)
image = models.ImageField(upload_to=upload_image_path, null=True, default=True)
featured = models.BooleanField(default=False)
objects = ProductManager()
def __str__(self):
return self.title
这是我得到的错误:
Page not found (404)
Request Method: GET
Request URL: https://ecommerce-hk967144.c9users.io/media/products/1299799540/1299799540.jpg
Raised by: django.views.static.serve
"/media/products/1299799540/1299799540.jpg" does not exist
有什么想法吗?
最佳答案
据我所知,错误似乎出在您的设置中。如果你看docs for static()
您可以看到您需要使用STATIC_ROOT
和MEDIA_ROOT
,但是您使用了*_URL
两次而不是使用*_ROOT
>.
更改这些行
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_URL)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_URL)
到此
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
但是,仅当您的 INSTALLED_APPS
中没有 'django.contrib.staticfiles'
时才需要使用 static()
;如果您的 INSTALLED_APPS
中确实有它,请删除这两行。
此外:
- 您不应使用
float
作为DecimalField
的默认值;使用Decimal('39.99')
代替(添加fromdecimal import Decimal
)。 - 您不应在
ImageField
中使用default=True
,因为True
不是有效的图像。
关于python - django 中找不到或不存在图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52454637/