Display images from a database in Django

1

I have a model that has an image field, at the beginning the image was shown, but after I made a few changes but without changing the code that serves the image now it does not work, I have tried everything but it does not work, this is the code of the setting:

setting.py
MEDIA_ROOT = 'media/'
MEDIA_URL = 'http://localhost:8000/media/'

this is the url.py     from django.views.static import serve

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

and this is my template         

I omit the other code because I consider it unnecessary for this.

    
asked by Wildy Estephan 17.10.2017 в 15:53
source

2 answers

2

I recommend that you take a look at the official documentation and try as follows: link

settings.py:

MEDIA_URL = '/media/'

urls.py:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    
answered by 17.10.2017 в 19:40
0

I have Django 1.11.6 with Python 3.6

In settings.py I have it like this:

MEDIA_ROOT = os.path.dirname(os.path.dirname(__file__))

MEDIA_URL = '/media/'

And in the URLs

from django.conf.urls.static import static

urlpatterns = [
# Examples:

url(r'^verarchivo/$', app.views.VerArchivosView, name='verArchivo'),

url(r'^contact$', app.views.contact, name='contact'),
url(r'^about', app.views.about, name='about')
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Or so but this was when I had Django 1.7 in the project

if settings.DEBUG:
# static files (images, css, javascript, etc.)
urlpatterns.append(
    # /media/:<mixed>path/
    url(
        regex=r'^media/(?P<path>.*)$',
        view='django.views.static.serve',
        kwargs={'document_root': settings.MEDIA_ROOT}))'''
    
answered by 19.10.2017 в 19:18