Django - 找不到静态文件 [英] Django - Static file not found

查看:148
本文介绍了Django - 找不到静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到这个问题的几个帖子,但没有找到我的解决方案。



我正在尝试在Django 1.3开发环境中提供静态文件。



这是我的设置

  ... 
STATIC_ROOT ='/ home / glide / Documents / django / cbox / static /'
STATIC_URL ='/ static /'
STATICFILES_DIRS =(
'/ static /',

...

我的urls.py

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

我的 / home / glide / Documents / django / cbox / static /目录就像

  css 
main.css
javascript
image

尝试访问时,我收到404错误http://127.0.0.1:8000/static/css/ main.css



我必须分别为css,javascript和图像指定模式吗?

解决方案

我混淆了 STATIC_ROOT STATICFILES_DIRS



其实我没有真正了解 STATIC_ROOT 的实用程序。我以为这是我必须放在我的公用文件的目录。此目录用于生产,这是静态文件将由 collectstatic



STATICFILES_DIRS 是我需要的。 >

由于我处于开发环境,因此我的解决方案是不要使用 STATIC_ROOT (或指定另一个路径)并设置我的公用文件目录在 STATICFILES_DIRS 中:

  #STATIC_ROOT =(os.path.join(SITE_ROOT,'static_files /' )
import os
SITE_ROOT = os.path.dirname(os.path.realpath(__ file__))
STATICFILES_DIRS =(
os.path.join(SITE_ROOT,'static /'),

还不要忘记从django.conf导入设置


I've seen several posts for this issue but didn't found my solution.

I'm trying to serve static files within my Django 1.3 development environment.

Here are my settings

...
STATIC_ROOT = '/home/glide/Documents/django/cbox/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
  '/static/',
)
...

My urls.py

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

My /home/glide/Documents/django/cbox/static/ directory is like

css
  main.css
javascript
image

I get a 404 error when trying to access http://127.0.0.1:8000/static/css/main.css.

Do I have to specify patterns for css, javascript and images individually ?

解决方案

I confused STATIC_ROOT and STATICFILES_DIRS

Actually I was not really understanding the utility of STATIC_ROOT. I thought that it was the directory on which I have to put my common files. This directory is used for the production, this is the directory on which static files will be put (collected) by collectstatic.

STATICFILES_DIRS is the one that I need.

Since I'm in a development environment, the solution for me is to not use STATIC_ROOT (or to specify another path) and set my common files directory in STATICFILES_DIRS:

#STATIC_ROOT = (os.path.join(SITE_ROOT, 'static_files/'))
import os
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = (
  os.path.join(SITE_ROOT, 'static/'),
)

Also don't forget to from django.conf import settings

这篇关于Django - 找不到静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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