A
A
azhlbn2018-11-02 22:02:43
Django
azhlbn, 2018-11-02 22:02:43

Why is media file link not generated in Django template?

I put a bootstrap-slider on the site and I want to be able to change images in it through the admin panel. Images are loaded through the admin panel and they open normally there. But the images don't appear in the html template. It's just that the html page doesn't understand that {{ slider.img1.url }} is a link to a media file.
urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('pixel.urls')),
]

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

settings.py
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(os.path.dirname(__file__),'static').replace('\\', '/'),
)

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

models.py
class slider(models.Model):
    img1 = models.ImageField(upload_to='slider_image', help_text='Размер изображение должен быть 1227 px на 550 px',
                                blank=True, null=True, verbose_name='Изображение 1')

    class Meta:
        verbose_name = 'Слайдер'
        verbose_name_plural = 'Слайдеры'

html page
<p style="text-align: center;"><img src="{{ slider.img1.url }}" width="200" height="200" class="img-thumbnail" align="center"></p>

Most likely, some kind of stupid mistake, because. I just recently started learning django, but I've been tormented all day, I can't figure out what I did wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2018-11-02
@dimonchik2013

src='{{ MEDIA_URL }}{{ record.imageurl }}'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question