Good day! Faced a problem. The HTML page does not display a picture because the url address is not correct. Here is the settings.py code

 import os

 # Build paths inside the project like this: os.path.join (BASE_DIR, ...)
 BASE_DIR = os.path.dirname (os.path.dirname (os.path.abspath (__ file__)))


 # Quick-start development settings - unsuitable for production
 # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

 # SECURITY WARNING: keep the secret key used in production secret!
 SECRET_KEY = 'y9xx15% xyrs $% utkpa) 7sbm! Zr & g5te & cfz_ (h ^ $ = uz & jla2b_'

 # SECURITY WARNING: don't run with debug turned on in production!
 DEBUG = True

 ALLOWED_HOSTS = []


 # Application definition

 INSTALLED_APPS = [
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     #Plugins
     'markdown',
     #myapps
     'display'
 ]

 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 = 'Portfolio.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 = 'Portfolio.wsgi.application'


 # Database
 # https://docs.dejangoproject.com/en/1.9/ref/settings/#databases

 DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.sqlite3',
         'NAME': os.path.join (BASE_DIR, 'db.sqlite3'),
     }
 }


 # Password validation
 # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

 AUTH_PASSWORD_VALIDATORS = [
     {
         'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
     },
     {
         'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
     },
     {
         'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
     },
     {
         'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
     },
 ]


 # Internationalization
 # https://docs.djangoproject.com/en/1.9/topics/i18n/

 LANGUAGE_CODE = 'Ru-ru'

 TIME_ZONE = 'Europe / Kiev'

 USE_I18N = True

 USE_L10N = True

 USE_TZ = True


 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/1.9/howto/static-files/

 STATIC_URL = '/ static_in_pro / out_static /'

 STATIC_ROOT = os.path.join (os.path.dirname (BASE_DIR), "static_in_pro", "static_root")

 STATICFILES_DIRS = [
     os.path.join (BASE_DIR, "static_in_pro", "out_static"),

 ]

 STATICFILES_FINDERS = (
     'django.contrib.staticfiles.finders.FileSystemFinder',
     'django.contrib.staticfiles.finders.AppDirectoriesFinder',
 )

 MEDIA_URL = '/ media_in_pro /'

 MEDIA_ROOT = os.path.join (os.path.dirname (BASE_DIR), "media_in_pro", "media_root")

Here is urls.py

 from django.conf import settings
 from django.conf.urls import url, include
 from django.conf.urls.static import static
 from django.contrib import admin

 urlpatterns = [
     url (r '^ admin /', admin.site.urls, name = admin),
     url (r '^', include ('display.urls', namespace = 'space_display')),
 ]

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

Here is the html

{% load staticfiles %} {% for project in Projects %} <div class="row" style="height:20%; border:4px solid yellow"> <div class="container-fluid"> <div class="row"> <div class="col-md-4"> <span><img src="{{ project.main_img.url }}" alt=""></span> </div> <div class="col-md-4" style="color:#33cccc;"> {{project.project_name}} {{project.stage}} {{project.date_begin}} {{project.date_end}} <br> </div> </div> </div> </div> {% endfor %} 

And here's the mistake

  GET http://192.168.1.10:8000/media_in_pro/projects/2016/09/27/project1.png 404 (Not Found) 
  • First, correct the last line of your urls.py - Nikmoon

2 answers 2

Thanks Nikmoon! Corrected!

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

    try to insert this line into the urlpatterns array in the main urls.py

     url(r'^media_in_pro/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}) 
    • Yes, he has the necessary line, just an error in it. - Nikmoon