【发布时间】:2019-04-16 03:56:32
【问题描述】:
在我的 setting.py 中,我有下一个代码:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, 'static'),
]
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
在views.py中,我想获取静态文件夹中的一些图片,如下所示:
for f in ["static/img/logo/contact-form/logo_black.png", "static/img/logo/contact-form/logo_white.png":
fp = open(os.path.join(BASE_DIR, f), 'rb')
msg_img = MIMEImage(fp.read())
fp.close()
msg_img.add_header('Content-ID', '<{}>'.format(f))
msg.attach(msg_img)
但我收到一个错误:
"[Errno 2] No such file or directory: '/Users/Admin/Projects/web-dealers/webDealers/static/img/logo/contact-form/logo-black.png'"
更新
urls.py如下:
urlpatterns = [
url(r'^django-admin/', admin.site.urls),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^search/$', search_views.search, name='search'),
url(r'^api/', include('API.urls')),
url(r'^contact-us/', contact_us, name="contact_us"),
# Languages
url(r'^i18n/', include('django.conf.urls.i18n'), name='set_language'),
url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
# For anything not caught by a more specific rule above, hand over to
# Wagtail's page serving mechanism. This should be the last pattern in
# the list:
url(r'', include(wagtail_urls)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
我做错了什么?
【问题讨论】:
-
您需要在 url 中定义它们才能访问它们,或者使用 NGINX 或 Apache 等外部服务来为它们提供服务
-
@ruddra 检查更新的问题。它们在 urls 中定义。
-
在视图中引用静态文件可能有问题。查看this question 讨论如何在视图中使用静态文件。
-
什么是“其余代码”?特别是,您是如何打开这些文件的?
-
@DanielRoseman 我已经更新了我的问题