Answer the question
In order to leave comments, you need to log in
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)
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/')
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 = 'Слайдеры'
<p style="text-align: center;"><img src="{{ slider.img1.url }}" width="200" height="200" class="img-thumbnail" align="center"></p>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question