python - 图像文件在 django 中找不到或不存在
问题描述
我正在使用django 1.11
.
我无法在页面上显示我的图像文件;它说
media/products/file-name does not exits
但我检查过,我的MEDIA_URL
和MEDIA_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
有任何想法吗?
解决方案
据我所知,错误似乎在您的设置中。如果您查看文档,static()
您会发现您需要使用STATIC_ROOT
and 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_APPS
INSTALLED_APPS
此外:
- 你不应该使用 a
float
作为你的默认值DecimalField
;改用Decimal('39.99')
(添加from decimal import Decimal
)。 - 你不应该使用
default=True
你的ImageField
,因为True
它不是一个有效的图像。
推荐阅读
- angular - 如何从 IHttpActionResult 数据在 Angular 中创建 blob 对象 (.PDF)?
- ansible - 如何为特定或自定义平台运行分子测试?
- ios - Swift async call and return
- sql - 如何在连接后创建一个虚拟列并填充一个数组?
- opengl-es - 如果在映射缓冲区上调用 glBufferData 会发生什么?
- php - 将变量与字符串进行比较不起作用
- java - 如果当前类是spring bean,如何使用抽象类的参数调用超级构造函数?
- docker - 我需要从詹金斯日志中隐藏参数
- swift - UICollectionViewCells 中的 UIPickerViews 中的 3 个 UILabel 之间不需要的交互
- wordpress - 在 Woocommerce 单品页面和商店页面中将产品 SKU 添加到产品标题