为什么我不能让我的静态目录与 django 1.3 一起使用?

发布于 2024-11-15 18:21:23 字数 1530 浏览 3 评论 0原文

我无法弄清楚将其

添加到我的 urlpatterns 中

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

,其中我的 main.css 是: /home/user/www/site/static/css/main.css

这个问题很简单,但当我访问 http://localhost:8000/static/

我得到:404:此处不允许目录索引。

当我访问 http://localhost:8000/static/css/main.css

我get: 404: 'css/main.css' 找不到

我做错了什么?

修复它:

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

在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/'

正如你所看到的,我真正改变的唯一事情是从STATIC_URL ='/static/'到STATIC_URL ='/mystatic/'

注意:当我到达http://localhost:8000/mystatic...我得到与上面相同的错误

我认为 STATIC_URL 应该是 '/static/' 这样您可以在模板中使用 {{ STATIC_URL }}...我真的不明白为什么此修复有效以及为什么我必须进行更改...

为什么这有效?

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

added to my urlpatterns

url(r'^static/(?P<path>.*)

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>.*)

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?

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


in settings.py


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?

, 'django.views.static.serve', {'document_root': settings.STATIC_ROOT } ),

in settings.py

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?

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

in settings.py

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?

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

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

发布评论

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

评论(3

涫野音 2024-11-22 18:21:23

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

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

一般来说,使用内置服务器,您不需要将 django.views.static.serve 添加到您的 url 中。

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

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/ 服务。

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

If you are using the built-in development webserver (i.e. run it with manage.py runserver), Django will take care of static files while in development.

Please note that STATIC_ROOT is the path where Django collects static files in, rather than the path that it serves files from. You should not maintain STATIC_ROOT yourself! You can read more on that in the Django documentation.

In general, you don't need to add django.views.static.serve to your urls, with the built-in server.

The static files should be placed elsewhere, besides STATIC_ROOT. You can place them either in the myapp/static path (i.e. under the individual app static file). You can also dedicate static folder for the entire project (e.g. /path/to/project/proj_settings) and update STATICFILES_DIRS in settings.py to be:

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

Then you can place your css/main.css file in /proj_static/css/main.css. Django built-in webserver will server /static/ from there.

While in production, you should collect all the static files in STATIC_ROOT, by running manage.py collectstatic. Then you can serve that folder directly through your webserver (e.g. nginx, Apache), rather than through Django.

以往的大感动 2024-11-22 18:21:23

主要是两步:

  1. 检查STATIC_ROOT路径:

    该文件存在吗? /home/user/www/site/static/css/main.css

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

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

  2. 在develope env中,(使用runserver启动Web服务器),确保:

    a)settings.py:已安装的应用程序包括:'django.contrib.staticfiles'

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

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

url(r'^static/(?P<path>.*)

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

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
, 'django.views.static.serve', {'document_root': settings.STATIC_ROOT } ),

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

mainly two step:

  1. check STATIC_ROOT path:

    Does the file exist? /home/user/www/site/static/css/main.css

    if not, you shoud run "python manage.py collectstatic" to copy static file to STATIC_ROOT path

    if "collectstatic" can't copy css files to STATIC_ROOT path, then shoud check source css path in "STATICFILES_DIRS"

  2. in develope env, (use runserver to start web server), make sure:

    a) settings.py: INSTALLED_APPS include: 'django.contrib.staticfiles'

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

I guess you not config step 2.b .
your method should ok, but it is hardcoded,

url(r'^static/(?P<path>.*)

In django's document it already be mentioned:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
, 'django.views.static.serve', {'document_root': settings.STATIC_ROOT } ),

In django's document it already be mentioned:

数理化全能战士 2024-11-22 18:21:23

您应该在 settings.py 中设置 STATIC_URL

在您的情况下,它必须是

STATIC_URL = '/static/'

You should set up STATIC_URL in settings.py

In your case it have to be

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