Django:找不到静态文件 [英] Django: static file not found

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

问题描述

在Windows主机中运行虚拟机guest虚拟机的Debian。设置桥接适配器类型。在客户端安装Django,并使用内置 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 

settings.py 中的调试设置为true(表示静态文件应该被保留) )。链接看起来正确。现在为什么这不工作?



编辑:看起来像我忘记运行收藏。这是运行它后的输出:





编辑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的帮助下解决了我的问题。有两个问题:


  1. 我没有运行 python manage.py collectstatic

  2. 配置错过了关键设置 - 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:

  1. I haven't ran python manage.py collectstatic.
  2. The config missed crucial settings - STATIC_ROOT and STATICFILES_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屋!

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