Django-静态图片无法加载,在控制台中引发404错误

时间:2018-08-24 03:49:13

标签: django

我很久以来一直在尝试解决此错误。我有很多试图通过网站打开的静态图像。但是,不会加载任何静态图像,并且当我在控制台中查看时,会看到错误Failed to load resource: the server responded with a status of 404 (Not Found)

有人知道为什么会这样吗?我已经多次运行collectstatic,重新启动服务器,完成了所有操作。

urls.py

from django.contrib import admin
from django.urls import path
import posts.views
import sitepages.views
import hackathons.views
import projects.views
import skills.views
from django.conf.urls.static import static
from django.conf import settings
from django.conf.urls import include

urlpatterns = [
    path('admin/', admin.site.urls),
    #path('blog/', posts.views.blog, name = "blog"),
    #path('posts/<int:post_id>/', posts.views.post_details, name="post_details"),
    path('about/', sitepages.views.about,name="about"),
    path('awards/',sitepages.views.awards,name="awards"),
    path('skills/',skills.views.skills,name="skills"),
    path('skills/<int:skill_id>/', skills.views.skill_details,name="skill_details"),
    path('projects/',projects.views.project,name="projects"),
    path('hackathons/',hackathons.views.hackathons,name="hackathons"),
    path('',sitepages.views.home,name="home"),
    #path('hobbies/',sitepages.views.hobbies,name="hobbies"),
    path('internships/',sitepages.views.internships,name="internships"),
    path('project_details/<int:project_id>/', projects.views.project_details,name="project_details"),
    path('hackathon_details/<int:hackathon_id>/', hackathons.views.hackathon_details, name="hackathon_details")
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

settings.py的静态部分

    """
Django settings for pranavblog project.

Generated by 'django-admin startproject' using Django 2.0.7.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!


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




# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'posts',
    'sitepages',
    'hackathons',
    'projects',
    'pagedown',
    'skills.apps.SkillsConfig',
    'django.forms'
]
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
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 = 'pranavblog.urls'
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(os.path.dirname(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',
            ],
        },
    },
]
WSGI_APPLICATION = 'pranavblog.wsgi.application'


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

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


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True
SITE_ID = 1

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

STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

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

以上是根据要求的整个settings.py。允许的主机和密钥被故意遗漏了。 如果需要更多信息,请告诉我。

编辑: 这是我的项目文件夹结构。媒体文件夹包含很多图像。 enter image description here

3 个答案:

答案 0 :(得分:0)

STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

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

答案 1 :(得分:0)

我相信您会错过此内容:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

也许不是,但这是我唯一能看到的您没有设置的东西。

答案 2 :(得分:0)

您已经添加了用于服务MediaFiles而非静态文件的路径。您可能需要将static(settings. STATIC_URL,document_root=settings. STATIC_ROOT)添加到urls.py

请勿在生产中使用DEBUG = True,这是一个很大的“否”。 您是否考虑过使用像NGINX这样的代理?您将能够缓存您的静态文件,从而提高性能。 Django并非旨在提供静态文件。