Django:找不到静态文件 [英] Django: static file not found
问题描述
runserver
和内置数据库进行测试。 简单文件结构:
> ..
>模板
> base.html
> static
> css
> base.css
> manage.py
> setting.py
>
文件 settings.py
:
STATIC_URL ='/ static /'
文件 base.html
:
{%load static%}
...
< link rel =stylesheethref ={%static'css / base.css'%}>
找不到错误:
GET http://192.168.XX.XX:8000 / static / css / base.css
设置为true(表示静态文件应该被保留) )。链接看起来正确。现在为什么这不工作? settings.py
中的调试
编辑:看起来像我忘记运行收藏。这是运行它后的输出:
编辑2:
EXTERNAL_APPS = [
..
'django.contrib
PROJECT_NAME ='fooproject'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__ file__)))
STATIC_ROOT = os.path.join(BASE_DIR,PROJECT_NAME,'staticfiles')
STATIC_URL ='/ static /'
我已经设法在ChristophBluoss和DanielRoseman的帮助下解决了我的问题。有两个问题:
- 我没有运行
python manage.py collectstatic
- 配置错过了关键设置 -
STATIC_ROOT
和STATICFILES_DIRS
将这些变量添加到settings.py
文件并成功运行<$ c $($)
$ c> collectstatic - 一切正常。
PS应该仔细阅读Django文档。对于我来说,没有足够明确的说明, collectstatic
是强制性的。
编辑: / strong>由于DanielRoseman建议 collectstatic
连同 STATIC_ROOT
是非强制性的,当 DEBUG
设置为 True
。我试图删除 staticfiles
和 STATIC_ROOT
- 它仍然适用于我的开发环境。所以 STATICFILES_DIRS
是缺少的设置。
Running Debian on Virtual Machine guest inside Windows host. Set Bridged for adapter-type. Installed Django on the guest and using build-in runserver
and built-in database for testing purposes.
Having simple files structure:
> ..
> templates
> base.html
> static
> css
> base.css
> manage.py
> setting.py
> ..
File settings.py
:
STATIC_URL = '/static/'
File base.html
:
{% load static %}
...
<link rel="stylesheet" href="{% static 'css/base.css' %}">
Getting error, not found:
GET http://192.168.XX.XX:8000/static/css/base.css
Debug
in settings.py
is set to true (means static files should be surved). The link looks correct. Now why doesn't this work?
Edit: looks like I forgot to run 'collectstatic'. Here is the output after running it:
Edit2:
EXTERNAL_APPS = [
..
'django.contrib.staticfiles',
]
PROJECT_NAME = 'fooproject'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, PROJECT_NAME, 'staticfiles')
STATIC_URL = '/static/'
I have managed to resolve my problem with the help from ChristophBluoss and DanielRoseman. There were 2 problems:
- I haven't ran
python manage.py collectstatic
. - The config missed crucial settings -
STATIC_ROOT
andSTATICFILES_DIRS
.
After adding those variables into settings.py
file and successfully running collectstatic
- everything worked fine.
P.S. One should read Django documentation more carefully. It wasn't stated clear enough for me that collectstatic
is the mandatory thing.
EDIT: As DanielRoseman suggested collectstatic
along with STATIC_ROOT
is non-mandatory when DEBUG
is set to True
. I have tried to remove staticfiles
and STATIC_ROOT
- and it still works in my development environment. So STATICFILES_DIRS
was the missing setting.
这篇关于Django:找不到静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!