使用--insecure后,Django图像无法加载

时间:2017-01-04 13:48:55

标签: python django web http-status-code-404 django-settings

我尝试制作自定义404错误页面。我将... {% block test_block %} {{ block.super }} {# Some more stuff to render #} {% endblock %} ... 更改为debug=True并将本地django服务器作为debug=False运行。这样做网站正在加载CSS但没有加载图像。使用>python manage.py runserver --insecure后,所有图像均未加载。使用debug = True加载所有css和图像。 我已经删除了一些额外部分的settings.py希望下面包含重要的部分。

settings.py

--insecure

urls.py

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DEBUG = False

ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'login',
'upload',
'ckeditor',
'ckeditor_uploader',
'django_bootstrap_carousel'
]

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

ROOT_URLCONF = 'MySite.urls'

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')]
    ,
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]

WSGI_APPLICATION = 'TorrentSite.wsgi.application'

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
DEFAULT_FROM_EMAIL = '-'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
EMAIL_PORT = 1025

MEDIA_ROOT = 'D:/projects/PyCharm projects/TorrentSite/media/'
MEDIA_URL = '/media/'

STATIC_ROOT = 'D:/projects/PyCharm projects/TorrentSite/upload/static/'
STATIC_URL = '/static/'

1 个答案:

答案 0 :(得分:1)

尝试在project / urls.py中添加它

...
from django.views.static import serve
...

urlpatterns = [
    ...
    url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT, }),
    ...
    ]