django 为什么我拍不到?

f0brbegy  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(101)

晚安
我想显示/media/images/ to store/ page中的图片,但我收到一个错误,即找不到该文件。
有几次 checkout 所有文件,但没有任何工作。
是哪里可能隐藏问题还是哪里可以找到解决的办法?

控制台输出

> python .\manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
November 19, 2023 - 04:16:07
Django version 4.2.7, using settings 'marketplace.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

[19/Nov/2023 04:16:10] "GET /store/ HTTP/1.1" 200 1282
Not Found: /media/images/Download-Icon.png
[19/Nov/2023 04:16:11] "GET /media/images/Download-Icon.png HTTP/1.1" 404 2278
[19/Nov/2023 04:16:11] "GET /store/ HTTP/1.1" 200 1282
Not Found: /media/images/Download-Icon.png

字符串

settings.py(示例网站)

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

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'store'
]

商店/Models.py

class Product(models.Model):
    title = models.CharField(max_length=64)
    description = models.TextField()
    images = models.ImageField(upload_to='images/')
    price = models.DecimalField(max_digits=7, decimal_places=2) # Max Value: 99,999.99
    sold = models.BooleanField()

    def __str__(self):
        return self.title

商店/Urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from . import views

urlpatterns = [
    path('', views.store_catalog, name='store_catalog'),
    path('about', views.store_info, name='store_info')
]

# In production, you would typically let your web server (like Nginx or Apache) handle serving static files.
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

商店/Views.py:

from django.shortcuts import render
from django.contrib.auth.models import User

from .models import Product

def store_catalog(request):

    products = Product.objects.all();

    return render(request, 'store/store_catalog.html', {"products": products})

def store_info(request):
    return render(request, 'store/store_info.html', {})

HTML模板:

{% load static %}
         <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">

         ....

        <section id="product-catalog">
            <h2>Catalog</h2>
            <div class="product-list">
                {% for product in products %}
                    <div class="product">
                        <img src="{{ product.images.url }}" alt="{{ product.title }}">
                        <h3>{{ product.title }}</h3>
                        <p><strong>₴</strong>{{ product.price }}</p>
                    </div>
                {% endfor %}
            </div>
        </section>

hpcdzsge

hpcdzsge1#

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')更改为MEDIA_ROOT = BASE_DIR /'media',然后尝试

相关问题