D
D
Dmitry Vyatkin2016-01-26 17:01:06
Django
Dmitry Vyatkin, 2016-01-26 17:01:06

How to correctly configure the distribution of MEDIA files in the Django settings?

I can not set up the distribution of MEDIA

settings.py 
MEDIA_ROOT = os.path.join(BASE_DIR, "files", "media")

MEDIA_URL = "/media/"

STATIC_ROOT = os.path.join(BASE_DIR, "files", "static")

STATIC_URL = "/static/"

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "templates", "static"),
]

STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

urls.py
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I'm trying to upload an image in a template, but it won't load.
<img src="/media/img.png">
Please help solve the problem!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Vyatkin, 2016-01-29
@dim137

The problem was in the urls.
it was like this:

urlpatterns = patterns ( 
    url(r'', ListView.as_view(template_name='homepage.html'),, name='index')
)

And you need it like this:
urlpatterns =  ( 
    url(r'^$', ListView.as_view(template_name='homepage.html'), name='index')
)

And then it will work and everything will be ok:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

T
ThemeZV, 2016-01-26
@ThemeZ

settings.py

STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = '/media/'

Quotes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question