django:找不到静态文件

时间:2011-07-02 17:11:28

标签: django django-urls

我遇到这个问题已经有一段时间了,并且无法解决这个问题。现在我正在使用django 1.2.4并进行以下设置:

AUTH_PROFILE_MODULE = 'customUsers.UserProfile'
TEMPLATE_STRING_IF_INVALID = 'Error generating variable'
DEBUG = True
TEMPLATE_DEBUG = DEBUG

ANONYMOUS_USER_ID = -1

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend', # default
    'guardian.backends.ObjectPermissionBackend',
)



MANAGERS = ADMINS        


TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
                                    "django.core.context_processors.debug",
                                    "django.core.context_processors.i18n",
                                    "django.core.context_processors.media",
                                   # "django.core.context_processors.static", there is no this function in the file
                                    "django.contrib.messages.context_processors.messages",
                                    "customUsers.user_cp_context.userCPContext")


USE_I18N = False

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = '/Users/carrier24sg/Documents/workspace/static_teachers/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/media/'

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/admin/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '(grqejktuccy6!@5pr#535*vivl#lcv06=v*hvae#&6mx15nzt'

# List of callables that know how to import templates from various sources.
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',
)

#ROOT_URLCONF = 'myproject.urls'

#TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
#   '/home/carrier24sg/webapps/django/myproject/templates'
#)


SITE_ID = 2

ROOT_URLCONF = 'teachers.urls'

TEMPLATE_DIRS = (
            '/Users/carrier24sg/Documents/workspace/templates',
            '/Users/carrier24sg/Documents/workspace/teachers/templates'
)



INSTALLED_APPS = (
                  'customUsers',
                  'ConsentForm',
                  'teachers.consent_teachers',
                  'django.contrib.auth',
                    'django.contrib.contenttypes',
                    'django.contrib.sessions',
                    'django.contrib.sites',
                    'django.contrib.messages',
                    'conversation',
                    'teachers.student_profiling',
                    'south',
                    'guardian',
                    'persistent_messages',)

由于某些原因,我无法提供像js和css这样的静态文件。开发服务器的输出显示404错误"GET /media/common/css/sidebar.css HTTP/1.1" 404 2202我试图在浏览器上输入静态文件的URL,而不是告诉我找不到该文件(我期待的那样),我看到了网址 - 无与伦比的django调试页面Using the URLconf defined in teachers.urls, Django tried these URL patterns, in this order: ......The current URL, media/js/conversation_load.js, didn't match any of these.

问题:为什么django没有像请求静态文件一样阅读网址http://127.0.0.1:8000/media/js/conversation_load.js

2 个答案:

答案 0 :(得分:1)

例如,您在settings.py

附近有“media”文件夹

然后尝试:

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

“媒体”是媒体文件夹名称。

在urls.py中:

from os import path
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': path.join(path.dirname(__file__), 'media')}),

答案 1 :(得分:0)

您是否已添加到模板中:

{% load staticfiles %}

这会加载所需的内容。