静态文件和媒体不会在生产中显示,但可以在开发环境中使用

时间:2020-07-22 09:00:16

标签: python django heroku django-templates django-staticfiles

该项目在开发中运行良好,但是当我将其推送到heroku应用程序时,媒体没有显示,这是我的代码 settings.py

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'), 
    os.path.join(BASE_DIR, "media"),
]

MEDIA_URL = '/media/'
STATIC_URL = '/static/'
MEDIA_ROOT = (
    os.path.join(BASE_DIR, 'media/')
)

STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')

urls.py

from django.contrib import admin
from django.urls import path
from rashed import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name="index"),
    path('form', views.Createview.as_view(), name="form")
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

0 个答案:

没有答案