为什么我不能让静态目录与django 1.3一起工作? [英] Why can't I get my static dir to work with django 1.3?

查看:241
本文介绍了为什么我不能让静态目录与django 1.3一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题很简单,但是我根本无法想象出来。



添加到我的urlpatterns

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

其中main.css是:/ home / user /当我访问

> http:// localhost:8000 / static /



我得到:404:这里不允许使用目录索引。



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



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

我做错了什么?



修正:

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

在settings.py

 #目录的绝对路径应该收集静态文件。 
#不要把任何东西放在这个目录下;在应用程序的static /子目录和STATICFILES_DIRS中存储静态文件
#。
#示例:/home/media/media.lawrence.com/static/
STATIC_ROOT = os.path.join(CURRENT_PATH,'static')#=='/ home / user / www / site / static'

#静态文件的URL前缀。
#示例:http://media.lawrence.com/static/
STATIC_URL ='/ mystatic /'

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



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



我以为STATIC_URL应该是'/ static /',以便您可以在模板中使用{{STATIC_URL}} ...我真的很不明白为什么这个修复工作,为什么我不得不作出我做的改变....



为什么这样做?

解决方案

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



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



一般来说,您不需要在您的网址中添加 django.views.static.serve STATIC_ROOT 中,静态文件应放置在其他位置。您可以将它们放在 myapp / static 路径中(即在单个应用程序静态文件下)。您还可以为整个项目(例如 / path / to / project / proj_settings )专用静态文件夹,并更新 STATICFILES_DIRS settings.py 中:

  STATICFILES_DIRS =(
#将字符串放在这里,如/ home / html / static或C:/ www / django / static
#总是使用正斜杠,甚至在Windows上
#不要忘记使用绝对路径,而不是相对路径
os.path.join(PROJECT_DIR,'proj_static'),

然后,您可以将 css / main.css 文件放在 /proj_static/css/main.css 。 Django内置的Web服务器将从那里服务器 / static /



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


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?

解决方案

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.

这篇关于为什么我不能让静态目录与django 1.3一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆