图像未在django中显示,但显示404错误

基兰

我遵循了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
纳比尔·艾哈迈德(Nabeel Ahmed)

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

图像未在Django中显示

来自分类Dev

图像未在Django 1.8中显示

来自分类Dev

图像未在Django Admin中显示

来自分类Dev

图像未在Django LFS中显示

来自分类Dev

Django 1.8中的ImageField类未在模板中显示图像

来自分类Dev

Django 1.8中的ImageField类未在模板中显示图像

来自分类Dev

图像未在ipython中显示

来自分类Dev

图像未在Codeigniter中显示

来自分类Dev

图像未在视野中显示

来自分类Dev

分页未在 Django 中显示

来自分类Dev

在Django中显示图像

来自分类Dev

Rails:Gravatar图像未在Chrome中显示

来自分类Dev

SVG图像元素未在Safari中显示

来自分类Dev

图像未在输入字段中显示

来自分类Dev

Flickr图像未在Colorbox模式中显示

来自分类Dev

PHP生成的图像未在WebView中显示

来自分类Dev

图像未在C#WPF中显示

来自分类Dev

图像未在模拟器中显示

来自分类Dev

导入的图像未在React中显示

来自分类Dev

绝对居中的图像未在Firefox中显示

来自分类Dev

SVG图像未在Firefox中显示

来自分类Dev

图像未在Form2中显示

来自分类Dev

背景图像未在Div中显示

来自分类Dev

图像未在angular2中显示

来自分类Dev

背景图像:未在 CSS 中显示

来自分类Dev

API验证错误未在Laravel中显示

来自分类Dev

未在PartialView表单中显示ModelState错误

来自分类Dev

未在表单(jsp)中显示错误消息

来自分类Dev

在PHP中显示图像错误