django:找不到静态文件

发布于 2024-11-18 08:11:33 字数 3962 浏览 2 评论 0原文

我遇到这个问题已经有一段时间了,并且无法解决这个问题。现在我正在使用 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,而不是告诉我发现找不到该文件(这是我所期望的),我看到了 url 不匹配的 django 调试页面使用在 Teachers.urls 中定义的 URLconf,Django 尝试了这些 URL 模式,按此顺序:......当前 URL,media/js/conversation_load.js,与其中任何一个都不匹配。

问题:为什么 django 不读取 url http:// 127.0.0.1:8000/media/js/conversation_load.js 就像静态文件的请求?

I ran into this problem for quite some time and is having trouble solving this. Right now I am using django 1.2.4 and having the following settings:

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',)

For some reasons I cannot serve static files like js and css. The output of the development server displays 404 error "GET /media/common/css/sidebar.css HTTP/1.1" 404 2202 I have tried entering the url for the static file on browser, instead of telling me that the file cannot be found(which i was expecting), I was shown the url-unmatched django debug page 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.

Question: why is django not reading the url http://127.0.0.1:8000/media/js/conversation_load.js like a request for static file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆橘 2024-11-25 08:11:33

例如,您的“media”文件夹靠近 settings.py

然后尝试:

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

其中“media”是您的媒体文件夹名称。

在 urls.py 中:

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

for example, you have your "media" folder near settings.py

Then try:

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

Where "media" is you media folder name.

In urls.py:

from os import path
(r'^media/(?P<path>.*)
, 'django.views.static.serve', {'document_root': path.join(path.dirname(__file__), 'media')}),
所有深爱都是秘密 2024-11-25 08:11:33

您是否已添加到模板中:

{% load staticfiles %}

这会加载所需的内容。

Have you added into your templates:

{% load staticfiles %}

This loads what's needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文