简体   繁体   中英

django 1.9 serve media files in production

i currently switched to django 1.9 (from 1.8.4) and my media files stoped

to be served.. ie every file that is uploaded by a user isn't served in the

templates - the images are not shown and the downloaded files can't be download..

what might be the problem?

settings:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'DIRS': [
            # insert your TEMPLATE_DIRS here
        ],
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

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


PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
MEDIA_URL = '/media/'

urls:

urlpatterns = [...] + static(settings.MEDIA_URL,   

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

template:

<img src="{{ obj.image.url }}" height="30%" width="25%">

this works for me (django 1.9.1)

in settings:

MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
in urls:
\n    if settings.DEBUG: \n        from django.conf.urls.static import static \n        urlpatterns += static(settings.MEDIA_URL, \n                              document_root=settings.MEDIA_ROOT) \n    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM