【问题标题】:Images are not found/loading correctly - 404未找到/正确加载图像 - 404
【发布时间】:2018-02-25 17:31:32
【问题描述】:

我正在使用Oscar-Django e-commerce项目,我正在关注教程Frobshop

网站已启动并正在运行,但是,当我从管理仪表板添加产品并上传图片时,缩略图未加载,当我在客户视图中查看产品时,图片丢失.

这是我在 settings.py 文件中的配置:

STATIC_URL = '/static/'
MEDIA_URL = '/媒体/'
MEDIA_ROOT = location("公共/媒体")
STATIC_ROOT = location('public/static')

当我从客户视图查看产品时,终端显示 404 GET

“GET /media/cache/45/ec/45ecfa8787510d3ed6997925b6e26ed7.jpg HTTP/1.1”404 4880

这就是它的样子

当我进入站点的管理部分并尝试从产品表中单击图片时,它也显示“找不到页面”,这次的URL是

http://127.0.0.1:8000/media/images/products/2017/09/hoodie1.jpeg

当我浏览到特定产品(仍在管理站点上)时,该产品的图像部分,缩略图不显示,终端显示此

“GET /media/cache/cd/8a/cd8af791d513ec91a583b0733070d9a7.jpg HTTP/1.1”404 4880

这是来自 URLs.py 的模式

urlpatterns = [ url(r'^i18n/', 包括('django.conf.urls.i18n')),

# The Django admin is not officially supported; expect breakage.
# Nonetheless, it's often useful for debugging.
url(r'^admin/', include(admin.site.urls)),

url(r'', include(application.urls)), ]

我在Finder中看到了这个路径下的图片

/frobshop/frobshop/public/media/images/products/2017/09

感谢任何帮助解决这个问题!

谢谢!

【问题讨论】:

    标签: python django django-oscar


    【解决方案1】:

    您需要在urls.py 的底部添加以下内容:

    from django.conf import settings
    if settings.DEBUG:
        from django.conf.urls.static import static
        from django.contrib.staticfiles.urls import staticfiles_urlpatterns
        # Serve static and media files from development server
        urlpatterns += staticfiles_urlpatterns()
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    这仅限于当settings.DEBUG==True 在生产环境中,您将希望使用 nginx 或等效工具来提供静态和媒体。

    这不是 Oscar 文档中涉及的内容,因为它是 Django 级别的问题,但可能应该提及。

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 2020-09-20
      • 2018-11-11
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      相关资源
      最近更新 更多