Django静态媒体不显示图片 [英] Django static media not showing picture

查看:71
本文介绍了Django静态媒体不显示图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找解决方案数小时后仍无法解决我的问题的情况后,我将其发布.我的媒体根目录中的图像未显示在我的html中.在chrome的控制台中,我得到了一个 404文件未找到.即使该图像在那里.我正在Pycharm中使用Python 3和Django 1.10.

after searching for a solution for hours which did not resolve my problem,I am posting this. The image from my media root is not showing up on my html. In chrome's console i get a 404 file not found.Even though the image is there. I am using Python 3 ,Django 1.10 in Pycharm.

这是模型,我将图像上传到其中:

This is the model which is where i upload images to:

from django.db import models

class Post(models.Model):
    username = "anonymous"
    post = models.ImageField(upload_to='anon')
    creation_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return Post.username

views.py :

from django.shortcuts import render,get_object_or_404
from .models import Post

def home(request):
    return render(request,"base.html",{})

def post_detail(request,id=None):
    instance = get_object_or_404(Post,id=id)
    context = {
        "post": instance.post,
        "instance": instance
    }
    return render(request,"post_detail.html",context)

post_detail.html (此处未显示图片):

post_detail.html(here the image isnt showing):

<body>
    <img src = "{{ instance.post.url}}" height="520" width="500"><br>
    {{ instance.creation_date }}<br>
    {{ instance.username }}<br>
</body>

setting.py 的部分:

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

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'Post.urls'

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

            ],
        },
    },
]

WSGI_APPLICATION = 'Post.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

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

我的目录如下:

推荐答案

混合静态设置和媒体设置是一个常见错误.在您的情况下,您实际上要处理的是用户上传的MEDIA,而不是STATIC.

It's a common mistake to mix up the static and media settings. In your case what you are actually dealing with is user uploaded MEDIA and not STATICs.

 <img src = "{{ instance.post.url}}" height="520" width="500"><br>

最相关的设置是此处描述的MEDIA_ *设置 https://docs.djangoproject.com/en/1.10/howto/static-文件/

The settings that are most relevent are MEDIA_* settings described here https://docs.djangoproject.com/en/1.10/howto/static-files/

但更重要的是,在您的开发服务器中,您需要通过添加

But more importantly, in your dev sever you need to enable the delivery of MEDIA by adding

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

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这篇关于Django静态媒体不显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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