Django不提供静态图像文件

时间:2017-08-18 23:58:06

标签: python django

Django目前没有在模板中显示profile.jpg文件。我查看了一些教程,但它没有解决问题。有关如何提供静态文件的任何建议吗?

我使用的是Django版本1.10.6

settings.py:

    """

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['*']


# Application definition

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

# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'
...


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

STATIC_URL = '/resume/static/'

这是我试图显示图像的模板内的线:

<img src="{% static 'resume/img/profile.jpg' %}" class="responsive-img" style='max-height:100px;' alt="face">

4 个答案:

答案 0 :(得分:0)

你不是说这是在生产环境还是非生产环境中。我假设它是生产,因为示例中有DEBUG = FALSE

如果是这种情况,您必须将您正在使用的任何一个http服务器指向static/文件夹所在的位置(如果您可以上传文件,请不要忘记media/)。这里有一个Apache示例:https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#serving-files

如果您正在开发中,请查看https://docs.djangoproject.com/en/1.10/howto/static-files/,您应该好好去!

答案 1 :(得分:0)

在使用静态文件之前,您需要在模板中使用以下行加载它们

{% load staticfiles %}

答案 2 :(得分:0)

尝试将此添加到您的设置

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static') 
STATIC_URL = '/resume/static/'
STATICFILES_DIRS = [os.path.join(PROJECT_ROOT, "staticfiles")]

答案 3 :(得分:0)

检查源代码,查看渲染图像网址的内容。 检查该网址是否有效。