Django 使用 Bootstrap,在 DEBUG = True 模式下,部分 js、css 提示 404 错误 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
imkh
V2EX    Django

Django 使用 Bootstrap,在 DEBUG = True 模式下,部分 js、css 提示 404 错误

  •  
  •   imkh 2016-04-12 17:23:53 +08:00 8063 次点击
    这是一个创建于 3469 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Django 1.8.3

    setings.py static 配置

    INSTALLED_APPS = ( ... 'django.contrib.staticfiles', ... ) TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\', '/')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'django.template.context_processors.static', ], }, }, ] STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') 

    使用的 Bootstrap 模板是 http://todc.github.io/todc-bootstrap/getting-started/

    项目目录结构

     ├── manage.py ├── static │ ├── bootstrap │ │ ├── css │ │ │ ├── bootstrap.css │ │ │ ├── bootstrap.css.map │ │ │ ├── bootstrap.min.css │ │ │ ├── bootstrap.min.css.map │ │ │ ├── bootstrap-theme.css │ │ │ ├── bootstrap-theme.css.map │ │ │ ├── bootstrap-theme.min.css │ │ │ ├── bootstrap-theme.min.css.map │ │ │ ├── todc-bootstrap.css │ │ │ ├── todc-bootstrap.css.map │ │ │ ├── todc-bootstrap.min.css │ │ │ └── todc-bootstrap.min.css.map │ │ ├── fonts │ │ │ ├── glyphicons-halflings-regular.eot │ │ │ ├── glyphicons-halflings-regular.svg │ │ │ ├── glyphicons-halflings-regular.ttf │ │ │ ├── glyphicons-halflings-regular.woff │ │ │ └── glyphicons-halflings-regular.woff2 │ │ ├── img │ │ │ └── checkmark.png │ │ └── js │ │ ├── bootstrap.js │ │ ├── bootstrap.min.js │ │ └── npm.js │ ├── css │ │ └── dashboard.css │ └── jquery │ └── jquery-2.2.3.min.js └── templates └── index.html 

    index.html 中的引用

     <head> ... <title>Dashboard Template for TODC Bootstrap</title> <!-- Bootstrap core CSS --> <link href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" rel="stylesheet"> <!-- TODC Bootstrap core CSS --> <link href="{{ STATIC_URL }}bootstrap/css/todc-bootstrap.min.css" rel="stylesheet"> <!-- Custom styles for this template --> <link href="{{ STATIC_URL }}css/dashboard.css" rel="stylesheet"> ... </head> <body> ... <script src="{{ STATIC_URL }}jquery/jquery-2.2.3.min.js"></script> <script src="{{ STATIC_URL }}bootstrap/js/bootstrap.min.js"></script> </body> ... 

    错误提示

    [12/Apr/2016 17:11:35]"GET / HTTP/1.1" 200 8953 [12/Apr/2016 17:11:36]"GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1" 304 0 [12/Apr/2016 17:11:36]"GET /static/css/dashboard.css HTTP/1.1" 404 1667 [12/Apr/2016 17:11:36]"GET /static/bootstrap/css/todc-bootstrap.min.css HTTP/1.1" 404 1724 [12/Apr/2016 17:11:36]"GET /static/bootstrap/js/bootstrap.min.js HTTP/1.1" 304 0 [12/Apr/2016 17:11:36]"GET /static/jquery/jquery-2.2.3.min.js HTTP/1.1" 404 1694 

    这个问题要怎么解决啊?

    18 条回复    2016-11-09 08:47:13 +08:00
    cc7756789
        1
    cc7756789  
       2016-04-12 17:30:14 +08:00   1
    怀疑是目录和文件权限的问题,
    lonelinsky
        2
    lonelinsky  
       2016-04-12 17:32:06 +08:00   1
    在项目的`urls.py`里面增加:
    ```python
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    urlpatterns += staticfiles_urlpatterns()
    ```
    试试?

    https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#static-file-development-view
    shenxgan
        3
    shenxgan  
       2016-04-12 17:33:07 +08:00
    可以试试这个:
    shenxgan
        4
    shenxgan  
       2016-04-12 17:34:05 +08:00   1
    @shenxgan 点错了
    可以试试这个:
    注释 `STATIC_ROOT`
    添加:
    ```python
    STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    )
    ```
    lonelinsky
        5
    lonelinsky  
       2016-04-12 17:39:23 +08:00
    @lonelinsky

    ```python
    # test markdown
    print('hello, markdown')
    ```
    imkh
        6
    imkh  
    OP
       2016-04-12 17:43:29 +08:00
    @cc7756789 不是
    imkh
        7
    imkh  
    OP
       2016-04-12 17:43:39 +08:00
    @lonelinsky 不行哦
    imkh
        8
    imkh  
    OP
       2016-04-12 17:43:55 +08:00
    @shenxgan 这样可以了 谢谢
    imkh
        9
    imkh  
    OP
       2016-04-12 17:44:15 +08:00
    @shenxgan 有没有相关的原理解释文档参考啊?
    imkh
        10
    imkh  
    OP
       2016-04-12 17:56:57 +08:00
    原来放在 app (不是 project )下使用 STATIC_ROOT = os.path.join(BASE_DIR, 'static'),才不会部分 css 、 js 文件出现 404 错误
    ```
    blogapp/
    ├── admin.py
    ├── form.py
    ├── __init__.py
    ├── migrations
    │ ├── 0001_initial.py
    │ ├── 0002_auto_20160411_1721.py
    │ ├── __init__.py
    ├── models.py
    ├── static
    │ ├── bootstrap
    │ │ ├── css
    │ │ │ ├── bootstrap.css
    │ │ │ ├── bootstrap.css.map
    │ │ │ ├── bootstrap.min.css
    │ │ │ ├── bootstrap.min.css.map
    │ │ │ ├── bootstrap-theme.css
    │ │ │ ├── bootstrap-theme.css.map
    │ │ │ ├── bootstrap-theme.min.css
    │ │ │ ├── bootstrap-theme.min.css.map
    │ │ │ ├── todc-bootstrap.css
    │ │ │ ├── todc-bootstrap.css.map
    │ │ │ ├── todc-bootstrap.min.css
    │ │ │ └── todc-bootstrap.min.css.map
    │ │ ├── fonts
    │ │ │ ├── glyphicons-halflings-regular.eot
    │ │ │ ├── glyphicons-halflings-regular.svg
    │ │ │ ├── glyphicons-halflings-regular.ttf
    │ │ │ ├── glyphicons-halflings-regular.woff
    │ │ │ └── glyphicons-halflings-regular.woff2
    │ │ ├── img
    │ │ │ └── checkmark.png
    │ │ └── js
    │ │ ├── bootstrap.js
    │ │ ├── bootstrap.min.js
    │ │ └── npm.js
    │ ├── css
    │ │ └── dashboard.css
    │ └── jquery
    │ └── jquery-2.2.3.min.js
    ├── templates
    │ └── blogapp
    │ └── index.html
    ├── tests.py
    ├── views.py

    ```

    放在 project 下的某个自定义项目(比如 project/static)的话,要使用
    ```
    STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    )
    ```
    zhuangzhuang1988
        11
    zhuangzhuang1988  
       2016-04-12 17:58:59 +08:00
    Pycharm debug 一下.. 基本就知道了./
    gamexg
        12
    gamexg  
       2016-04-12 20:08:14 +08:00 via Android
    Django 的静态文件不是需要使用命令合并的吗?
    各个 app 有自己的 static 文件夹,需要使用命令合并成为一个。
    ericFork
        13
    ericFork  
       2016-04-12 21:18:56 +08:00
    @gamexg 这个是 DEBUG = False 时,以便其他 webserver 来 serve 静态文件
    在 DEBUG = True 模式下是可以用内建 server 的
    imkh
        14
    imkh  
    OP
       2016-04-12 23:25:56 +08:00
    @ericFork 是的
    shenxgan
        15
    shenxgan  
       2016-04-13 08:26:22 +08:00   2
    @imkh
    1. 执行 collectstatic 一般是在生产环境中使用,将所有的静态文件汇集到你指定的 STATIC_ROOT 目录下。这样做的目的是因为在生产环境(比如我使用 nginx )中一般只会指定一个静态文件路径。
    2. 在开发环境( debug )中,查找静态文件的顺序是:先在 STATICFILES_DIRS 里面找,然后在各个 APP 下的 static 目录下找,有同名的文件不要紧,它只会使用找到的第一个。看看官网描述: https://docs.djangoproject.com/en/1.8/ref/settings/#staticfiles-finders
    Fred84
        16
    Fred84  
       2016-04-13 14:10:13 +08:00   1
    @imkh shenxgan 说的是对的,你可以看看我的这个总结文章: http://fredzc.com/blog/8_20160325160441.html
    crazycookie
        17
    crazycookie  
       2016-11-09 08:46:25 +08:00
    collectstatic 到你 static 文件下, 如果是 nginx ,还要把 static 这个文件夹别名下,这样就完善了,因为 动态和静态分离
    crazycookie
        18
    crazycookie  
       2016-11-09 08:47:13 +08:00
    python manage.py runserver 开发模式下,框架会自动模拟,但是 到了 debug=False, 就不会用框架给你找静态文件了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2376 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 82ms UTC 01:25 PVG 09:25 LAX 18:25 JFK 21:25
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86