首页 > 解决方案 > 图像文件在 django 中找不到或不存在

问题描述

我正在使用django 1.11.

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

media/products/file-name  does not exits

但我检查过,我的MEDIA_URLMEDIA_ROOT是正确的。

设置.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")

网址.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)

模型.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

有任何想法吗?

标签: pythondjango

解决方案


据我所知,错误似乎在您的设置中。如果您查看文档,static()您会发现您需要使用STATIC_ROOTand 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)

但是,只有在static()您没有 ; 时才需要使用; 如果您确实有它,请删除这两行。'django.contrib.staticfiles'INSTALLED_APPSINSTALLED_APPS


此外:

  • 你不应该使用 afloat作为你的默认值DecimalField;改用Decimal('39.99')(添加from decimal import Decimal)。
  • 你不应该使用default=True你的ImageField,因为True它不是一个有效的图像。

推荐阅读