Django没有看到静态文件图像

时间:2019-04-21 06:32:12

标签: django

我正在尝试使用Django创建在线商店。我想添加商品照片,但是Django由于某种原因没有看到它们。请帮助解决问题。

以下是错误的屏幕截图:enter image description here

这是 settings.py

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles' 
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    '../static/products/media/product_images/',
)

models.py

from django.db import models

# Create your models here.
class Product(models.Model):
    name = models.CharField(max_length=70, blank=True, null=True, default=None)
    price = models.DecimalField(max_digits=10, decimal_places=2, default=0)
    description = models.TextField(blank=True, null=True, default=None)
    short_description = models.TextField(blank=True, null=True, default=None)
    is_active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return "%s, %s" % (self.price ,self.name)

    class Meta:
        verbose_name = 'Товар'
        verbose_name_plural = 'Товары'


class ProductImage(models.Model):
    product = models.ForeignKey(Product, blank=True, null=True, default=None, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='static/media/product_images/')
    is_active = models.BooleanField(default=False)
    is_main = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __str__(self):
        return "%s" % self.id

    class Meta:
        verbose_name = 'Фотография'
        verbose_name_plural = 'Фотографии'

urls.py 文件

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('online_shop.urls', namespace='online_shop')),
    path('', include('products.urls', namespace='products')),
    path('', include('orders.urls', namespace='orders')),
]

html模板

{% extends 'online_shop/base.html' %}
{% load static %}

{% block content %}
    <section>
        <div class="top-section">
            <img src="{% static 'img/clem.png' %}" class="img-fluid">
        </div>
    </section>
    <section>
        <div class="container">
            <div class="row">

                {% for product_image in product_images %}
                    <div class="col-lg-3">
                        <div class="product-item">
                            <div>
                                <img src="{{product_image.image}}" alt="" class="img-fluid">
                            </div>
                            <h4>{{product_image.product.name}}</h4>
                            <p>{{product_image.product.description|truncatechars_html:80 }}</p>
                            <div class="price">
                                {{product_image.product.price}} ГРН
                            </div>
                            <div class="add-to-card">
                                <button class="btn btn-success">
                                    Добавить в корзину
                                </button>
                            </div>
                        </div>
                    </div>
                {% endfor %}

            </div>
        </div>
    </section>
{% endblock content %}

2 个答案:

答案 0 :(得分:0)

您可以通过将以下代码段添加到urls.py中来做到这一点

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('online_shop.urls', namespace='online_shop')),
    path('', include('products.urls', namespace='products')),
    path('', include('orders.urls', namespace='orders')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

答案 1 :(得分:0)

您可能需要做一些修改

  

settings.py

# prefix used in static files path rendering
STATIC_URL = '/static/'

# store static files once execute python manage.py collectstatic
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')

# directories where static files are stored in the development environment
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

# prefix used upon uploaded images
MEDIA_URL = '/media/'

# where uploaded images should save
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
  

urls.py

# lets you have the ability to view images even in development environment 
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

更新: 由于您使用static/media/product_images/作为上传路径,因此上传的文件将保存在 project-root-dir/media/static/products/media/product_images

希望这会有所帮助!