0

I'm new to Django (1.5) and I'm having a hard time to relative configuration of the media folder in MEDIA_ROOT. I can't charge the files .css, .js, .jpg in my project. I receive the following msg in the shell:

[06/Oct/2013 19:12:01] "GET /media/css/style.css HTTP/1.1" 404 4140

this is tree of the project:

  • _3Ms
    • _3Ms
    • apps
    • media
      • css
      • js
      • images
    • template

this is my configuration

setting.py

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media/')

MEDIA_URL = '/media/'

STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIRS = (
)


STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = '_3Ms.urls'

WSGI_APPLICATION = '_3Ms.wsgi.application'

TEMPLATE_DIRS = os.path.dirname(__file__), 'templates',
)

home.html

{% block css %}<link href="/media/css/style.css" rel="stylesheet">{% endblock %}

Thank you in advance

4

1 回答 1

3

您通常使用STATIC_URLCSS 和 Javascript 等静态资产。媒体用于管理员或用户上传的文件。

所以填写你的STATICFILES_DIRSTATIC_URL在你的模板中使用。

设置:

import os.path

PROJECT_PATH = os.path.abspath(os.path.join(os.path.split(__file__)[0], '..'))

STATICFILES_DIRS = [
    '/path/to/your/static/assets',
    os.path.abspath(os.path.join(PROJECT_PATH, '..', 'static')),
]

模板:

{% block css %}<link href="{{ STATIC_URL }}css/style.css" rel="stylesheet">{% endblock %}

我假设您正在使用 Django 开发服务器。

于 2013-10-06T18:00:59.657 回答