首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Django看不到静态文件图像

Django看不到静态文件图像
EN

Stack Overflow用户
提问于 2019-04-21 14:32:12
回答 1查看 80关注 0票数 0

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

以下是错误的屏幕截图:

这是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 %}
EN

回答 1

Stack Overflow用户

发布于 2019-04-21 16:09:53

您可以通过将以下代码片段添加到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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55780474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档