0

我正在开发一个需要在生产中设置的博客,当我将其设置为生产时问题就来了。所有静态文件都可以完美加载,但我上传的图片除外。在将其设置为生产之前,我也尝试过使用之前上传的图像,之后也是如此。 网页截图

设置.py

DEBUG = False

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'core.apps.CoreConfig',
    'unities.apps.UnitiesConfig',
    'ckeditor',
    'ckeditor_uploader',
    'django_cleanup.apps.CleanupConfig'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

...

STATIC_URL = '/static/'

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

STATICFILES_DIRS=(
    os.path.join(BASE_DIR,'static'),
)

STATICFILES_STORAGE='whitenoise.storage.CompressedManifestStaticFilesStorage'

# Media files

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

# CKEditor Settings

CKEDITOR_ALLOW_NONIMAGE_FILES = False
CKEDITOR_UPLOAD_PATH = 'ckeditor_uploads/'

网址.py

urlpatterns = [
    path('ckeditor/', include('ckeditor_uploader.urls')),
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
    path('<slug:unity_slug>/', include('unities.urls')),
]

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

我正在使用 pythonanywhere

4

1 回答 1

0

您是否在urls.py顶部导入了静态文件?我用django3.0

from django.conf.urls.static import static

对于urls.py的缺点,我会这样编码

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

对于settings.py 我只写这样的代码

STATIC_URL = '/static/'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
于 2020-11-27T01:19:03.443 回答