S
S
serik2018-03-29 15:43:27
Django
serik, 2018-03-29 15:43:27

Media images not showing in django?

settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

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

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('annex.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models.py
class Company(models.Model):
    name = models.CharField("Название компании", max_length = 80)
    city = models.CharField("Город", max_length = 80)
    background = models.ImageField("Фон", upload_to="annex/img/%Y/%m/%d", default='')

template.html
{% load static %}
{% block content %}
<div class="banner-bg --not_banner" id="top">
{% for items in object_list %}
  <div class="with-company col-md-4">
    <div class="col-xs-12">
      <div class="banner-overlay"></div>
      <div class="content-company">
        <div style="background: url({{items.background.url}});" class="welcome-text">
...

The static works, it displays the text from the database, but it swears with the media.
I check, in the browser the link is displayed correctly, when I try to follow the link, I get a 404 error

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlexandrBirukov, 2018-03-29
@AlexandrBirukov

style="background: url('{{items.background.url}}');" - in quotes {{items.background.url}} take

D
Dimonchik, 2018-03-29
@dimonchik2013

{% get_media_prefix %}
works?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question