图像未在django中显示,但显示404错误 [英] Image Not displaying in django getting 404 error

查看:71
本文介绍了图像未在django中显示,但显示404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I followed official django documentation for image field.I am able to store the image in the database but when I try to display it in browser I am getting 404 error.

我的设置文件,

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文件,

My models.py file,

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

我的views.py文件,

My views.py file,

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文件的小部分,

Small part of my urls.py file,

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控制台中的错误,

This is the error in the eclipse console,

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

推荐答案

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

In urls.py, I am noticing it has to be:

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

静态(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)

这篇关于图像未在django中显示,但显示404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆