7

This problem is very simple, but I just can't figure it out

added to my urlpatterns

url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/home/user/www/site/static'})

where my main.css is : /home/user/www/site/static/css/main.css

when I access http://localhost:8000/static/

I get: 404: Directory indexes are not allowed here.

when I access http://localhost:8000/static/css/main.css

I get: 404: 'css/main.css' could not be found

What am I doing wrong?

Fixed it:

url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT } ),

in settings.py

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(CURRENT_PATH, 'static') #=='/home/user/www/site/static'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/mystatic/'

As you can see the only thing I really changed was from STATIC_URL = '/static/' to STATIC_URL = '/mystatic/'

note: when I got to http://localhost:8000/mystatic... I get the same errors as above

I thought that STATIC_URL was supposed to be '/static/' so that you could use {{ STATIC_URL }} in your templates... I really don't understand why this fix worked and why I had to make the change that I did....

Why does this work?

4

3 回答 3

9

如果您使用内置的开发网络服务器(即使用 运行它manage.py runserver),Django 将在开发时处理静态文件。

请注意,这STATIC_ROOT是 Django 收集静态文件的路径,而不是它提供文件的路径。你不应该维护STATIC_ROOT自己!您可以在Django 文档中阅读更多相关信息。

通常,您不需要django.views.static.serve使用内置服务器添加到您的 url。

静态文件应该放在其他地方,除了STATIC_ROOT. 您可以将它们放置在myapp/static路径中(即在单个应用程序静态文件下)。您还可以为整个项目(例如/path/to/project/proj_settings)专用静态文件夹并更新STATICFILES_DIRSsettings.py

STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_DIR, 'proj_static'),
)

然后你可以把你的css/main.css文件放在/proj_static/css/main.css. Django 内置的网络服务器/static/将从那里服务器。

在生产中,您应该STATIC_ROOT通过运行收集所有静态文件manage.py collectstatic。然后,您可以直接通过您的网络服务器(例如 nginx、Apache)而不是通过 Django 来提供该文件夹。

于 2011-06-15T21:14:30.787 回答
1

主要是两步:

  1. 检查 STATIC_ROOT 路径:

    文件是否存在?/home/user/www/site/static/css/main.css

    如果没有,您应该运行“python manage.py collectstatic”将静态文件复制到 STATIC_ROOT 路径

    如果“collectstatic”无法将 css 文件复制到 STATIC_ROOT 路径,则应检查“STATICFILES_DIRS”中的源 css 路径

  2. 在开发环境中,(使用 runserver 启动 Web 服务器),确保:

    a) settings.py: INSTALLED_APPS 包括: 'django.contrib.staticfiles'

    b) urls.py: urlpatterns += staticfiles_urlpatterns()

我猜你没有配置步骤 2.b 。你的方法应该没问题,但它是硬编码的,

url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT } ),

在 django 的文档中已经提到过:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
于 2011-07-20T09:22:50.243 回答
0

您应该在 settings.py 中设置 STATIC_URL

在你的情况下,它必须是

STATIC_URL = '/static/'
于 2011-06-15T20:20:21.967 回答