Why can't I get my static dir to work with django 1.3?

为君一笑 提交于 2019-12-03 10:58:42

问题


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?


回答1:


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.




回答2:


You should set up STATIC_URL in settings.py

In your case it have to be

STATIC_URL = '/static/'



回答3:


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>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT } ),

In django's document it already be mentioned:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()


来源:https://stackoverflow.com/questions/6363728/why-cant-i-get-my-static-dir-to-work-with-django-1-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!