简体   繁体   中英

Django not serving static image file

Django is currently not displaying the profile.jpg file in the template. I looked at some tutorials but it didn't solve the issue. Any suggestions on how to serve the static files?

I'm using Django version 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/'

Heres the line inside the template where I'm trying to display the image:

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

you're not saying if this is in a production or not-production environment. I assume it is production as you have DEBUG = FALSE in the example.

If that's the case you will have to point whichever http server you're using to where your static/ folder lives (and do not forget about media/ if you can upload files). Here you have an example for Apache: https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#serving-files

If you're under development, take a look at https://docs.djangoproject.com/en/1.10/howto/static-files/ and you should be good to go!

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

{% load staticfiles %}

Try adding this to your settings

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")]

Inspect the source code, see what's the rendered image url. Check whether that url is valid or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM