A
A
Andrei Ramanchyk2017-04-02 04:30:06
Django
Andrei Ramanchyk, 2017-04-02 04:30:06

Django 1.10 Why doesn't the image from the media folder show up?

There is a model

class ImageModule(models.Model):
    module = models.ForeignKey(Module)
    image_description = models.CharField(max_length=30, default='Photo object')
    image_module = models.ImageField(blank=True)

    def __str__(self):
        return self.image_description

view.py
class ModuleView(DetailView):
    model = Module
    template_name = 'module.html'

    def get_context_data(self, **kwargs):
        context = super(ModuleView, self).get_context_data(**kwargs)
        images = ImageModule.objects.filter(module=self.kwargs['pk'])
        context['images'] = images
        return context

there is urls.py
from django.conf.urls import url, include
from django.contrib import admin
from design_engineer.views import *
from django.conf.urls.static import static
from django.conf import settings
from django.views.static import serve
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib.staticfiles import *


urlpatterns = [
    url(r'^module/project_objectives/(?P<pk>\w)/$', ProjectObjectivesView.as_view(), name='project_objectives'),
    url(r'^module/(?P<pk>\w)/$', ModuleView.as_view(), name='module'),
    url(r'^admin/', admin.site.urls),
    url(r'^rooms/(?P<pk>\w+)/$', RoomView.as_view(), name='rooms'),
    url(r'^projects/(?P<pk>\w+)/$', ProjectsView.as_view(), name='projects'),
    url(r'', HomeView.as_view(), name='home'),
    # url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})
]


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

setting.py
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.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 's!nf-nl6rg#c6%a3uneg3#34^&%hgb$y#in^jkku-2exmobkhh'

# 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',
    'design_engineer',
]

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',
]

ROOT_URLCONF = 'workplace_site.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',
                'django.template.context_processors.media',
            ],
        },
    },
]

WSGI_APPLICATION = 'workplace_site.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/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.10/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.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

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


try:
    from workplace_site.local_settings import *
except ImportError:
    pass

html
{% extends "basic_page.html" %}
{% block title %}
    <title>Module</title>
{% endblock %}

{% block body %}
    <h1>Произведен? {{ object.module_is_produced }}</h1>
    <ul>
        <li>{{ object.room }}</li>
        <li>{{ object.module_name }}</li>
        <li>{{ object.module_designation }}</li>
        <li>{{ object.module_description }}</li>
    </ul>
    <h2><a href={% url 'project_objectives' object.pk%}>Необходимые доработки</a></h2>

    {% for im in images %}
        {{ im.image_description }}"
        {{ im.image_module.url }}
        <img src="{{ im.image_module.url }}">
        <img src="{{MEDIA_URL}}" />
    {% endfor %}
{#    {{ image.image_module.url }}#}
{#    <img src="{{ image.image_module.url }}">#}
{% endblock %}

The picture is not displayed, only
025aa311284044fa80a496b0846c149e.JPG

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-04-02
@jagrmi

The route url(r'', HomeView.as_view(), name='home'),is suitable for any url, therefore it overlaps everything after it, including those starting with /media/. Change it to r'^$'.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question