Why doesn't the Django pictures show?



  • There's a problem here: a picture loaded through the adminca is not on the main page:

    введите сюда описание изображения

    Here's the view code.py.

    class IndexView(generic.ListView):
    template_name = 'Homepage/index.html'
    model = Goods
    context_object_name = 'goods'
    

    def description(self):
    return self.description_text

    def price(self):
    return self.price_text

    def image(self):
    return self.image_sale

    Here's the code from models.py:

    class Goods(models.Model):
    description_text = models.CharField(max_length=200)
    price_text = models.CharField(max_length=200)
    image_sale = models.ImageField(blank=True, upload_to='media/')

    def str(self):
    return self.image_sale

    def str(self):
    return self.description_text

    def str(self):
    return self.price_text

    Here's way of pictures in settings.

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

    this is what the index template looks.html.

    {% if good.image %}
    <img src="{{ MEDIA_URL }}{{ good.image_sale }}">
    {% endif %}

    That's what the media looks like:
    введите сюда описание изображения



  • You're going to check all the structures. ♪ ♪

    setting.py:

    import os
    

    TEMPLATES = [
    {
    ...
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    ...
    },
    ]

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

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

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

    urls.py:

    from django.conf import settings
    from django.conf.urls.static import static
    from django.contrib import admin
    from django.urls import path, include

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

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

    models.py:

    from django.db import models

    class Goods(models.Model):
    description_text = models.CharField(max_length=200)
    price_text = models.CharField(max_length=200)
    image_sale = models.ImageField(blank=True, upload_to='images/')

    def __str__(self):
        return self.description_text
    
    def __str__(self):
        return self.price_text
    

    views.py:

    from django.views.generic import ListView
    from .models import Goods

    class HomeView(ListView):
    model = Goods
    template_name = 'index.html'

    urls.py Annexes:

    from django.urls import path
    from . import views

    urlpatterns = [
    path('', views.HomeView.as_view(), name='home'),
    ]

    index.html:

    <!DOCTYPE html>
    <html lang="ru">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Картинка</title>
    </head>
    <body>
    {% for goods in goods_list %}
    <img src="{{ goods.image_sale.url }}" alt="">
    {% endfor %}
    </body>
    </html>

    Here's the structure of the annex. image screen I've got all the downloaded pictures through the adminca on the page. Don't forget to squeeze.

    requirements.txt:

    asgiref==3.4.1
    Django==3.2.9
    Pillow==8.4.0
    pytz==2021.3
    sqlparse==0.4.2

    Add below a piece of the code that the author has worked on:

    <body>
    {% for good in goods %}
    <img src="{{ good.image_sale.url }}" alt="">
    {% endfor %}
    </body>



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2