在 Django 中找不到或不存在图像文件



我正在使用django 1.11.

我无法在页面上显示我的图像文件;它说

media/products/file-name  does not exits

但我检查了一下,我的MEDIA_URLMEDIA_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

有什么想法吗?

据我所知,错误似乎在您的设置中。如果您查看static()的文档,您会发现您需要使用STATIC_ROOTMEDIA_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')(添加from decimal import Decimal(。
  • 您不应该将default=True用于ImageField,因为True不是有效的图像。

相关内容

  • 没有找到相关文章

最新更新