Django images not loading even after using --insecure

626 views Asked by At

I tried to make custom 404 error page. I changed debug=True to debug=False and run local django server as >python manage.py runserver --insecure. Doing so website is loading css but didn't load images. All the images didn't load even after using --insecure. With debug=True all of css and images were loading. I have removed some extra portion of settings.py hope below covers important ones.

settings.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/'

urls.py

from django.conf.urls import url, include
from django.contrib import admin
from upload import views
from TorrentSite import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

handler404 = 'upload.views.custom_404'

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home_page, name='home_page'),
url(r'^games/', include('upload.urls')),
url(r'^contact/', views.contact_page, name='contact_page'),
url(r'^search/$', views.search_result, name='search-result')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()
1

There are 1 answers

0
Sergei Malanin On BEST ANSWER

try add this in project/urls.py

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

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