【问题标题】:Image Not displaying in django getting 404 error图像未在 django 中显示得到 404 错误
【发布时间】:2016-05-09 07:02:46
【问题描述】:

我遵循了图像字段的官方 django 文档。我能够将图像存储在数据库中,但是当我尝试在浏览器中显示它时出现 404 错误。

我的设置文件,

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,"templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.media',
                'django.core.context_processors.static',
            ],
        },
    },
]

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static")
MEDIA_ROOT =  os.path.join(os.path.dirname(BASE_DIR),"media")
MEDIA_URL = '/media/'

我的 models.py 文件,

class Add_prod(models.Model):
    book = models.CharField("Book Name",max_length=40)
    image = models.ImageField(upload_to='static/images',null=True)

我的views.py文件,

def add_prod(request):
    form = ProdForm(request.POST or None,request.FILES or None)
    my_products = Add_prod.objects.all()
    context = {
            "form":form,
            "products":my_products
    }
    if form.is_valid():
        instance = form.save(commit=False)
        book = form.cleaned_data.get("book")
        image = form.cleaned_data.get("image")
        instance.book = book
        instance.image = image
        instance.save()
   return render(request,"add-prod.html",context)

我的 urls.py 文件的一小部分,

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

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

我的模板文件的一小部分,

{% for i in products %}  
            <tr>
                <td>{{i.book}}</td>
                <td><img src="{{ MEDIA_URL }}{{i.image.url}}" alt="No Image"></td>
            </tr>
{% endfor %}

这是eclipse控制台的错误,

Not Found: /media//media/static/images/644.jpg
"GET /media//media/static/images/644.jpg HTTP/1.1" 404 1800

【问题讨论】:

  • /media 翻倍。它不应该。你确定{{i.image.url}} 还没有包含{{ MEDIA_URL }}
  • 您不需要{{ MEDIA_URL }} 中的src。这是在生成的路径中复制/media/。删除它,它应该可以工作。
  • 现在我收到此错误,未找到:/media/static/images/644.jpg
  • 媒体文件夹在我的项目文件夹旁边,应该在项目文件夹里面吗?
  • 在 urls.py 中,我注意到它必须是 - static(settings.STATIC_URL,document_root=settings.STATIC_ROOT) 和 static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

标签: python django django-templates


【解决方案1】:

urls.py,我注意到它必须是:

static(settings.STATIC_URL,document_root=settings.STATIC_ROOT) 

static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

【讨论】:

    猜你喜欢
    • 2022-01-18
    • 2017-08-27
    • 2021-02-28
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 2015-05-17
    相关资源
    最近更新 更多