简体   繁体   English

django不提供静态文件。 404错误

[英]django not serving static files. 404 error

I'm new to the django framework and I cannot get it to serve static files. 我是django框架的新手,我无法使用它来提供静态文件。 I'm following a website tutorial here . 我在这里关注网站教程。 The templates folder loads properly. template文件夹正确加载。 I cannot access localhost:8000/static/ 我无法访问localhost:8000 / static /

Here's the settings.py 这是settings.py

    import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')

TEMPLATE_DIRS = (
    TEMPLATE_PATH,
    )

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '626+nh90b61sb)6@bp(v!o5g&&()s)g!@k*ay$tor!$nln88#*'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rango',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'tangoWithDjangoProject.urls'

WSGI_APPLICATION = 'tangoWithDjangoProject.wsgi.application'


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

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

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

STATIC_PATH = os.path.join(BASE_DIR, 'static')


STATICFILES_DIR = (
     STATIC_PATH,
    )

Here's index.html where I want to load the image 这是index.html我要在其中加载图像的位置

   <!DOCTYPE html>

{% load static %}

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Rango Index</title>
</head>
<body>
    <h1>Rango Index page</h1>
    <p><strong>{{boldmessage}}</strong></p>
    <p><a href="/rango/about/">About</a></p>
    <img src="{% static "images/steve.jpg" %}"/>
</body>
</html>

settings.py settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__) + '/../')

STATIC_URL = '/static/'
STATIC_ROOT = PROJECT_DIR +  '/static/'
MEDIA_URL =  '/media/'
MEDIA_ROOT = PROJECT_DIR + '/media/'

urls.py 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_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static('/templates/', document_root=settings.PROJECT_DIR + '/templates/')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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